
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>04 服务器端db_proxy_server源码分析 · 高性能服务器开发 技术专栏</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="easy_coder">
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-chapter-fold/chapter-fold.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search-pro/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-back-to-top-button/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-prism/prism.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-code/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-anchor-navigation-ex/style/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-theme-fexa/fexa.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="05服务器端msg_server源码分析.html" />
    
    
    <link rel="prev" href="03服务器端的程序架构介绍.html" />
    

    </head>
    <body>
        
<div class="book">
	<div class="header-inner">
		<!-- LOGO -->
		<div class="logo"></div>
		<span class="title"></span>

		<!-- Search -->
		
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>


		<!-- Nav -->
		<ul class="header-nav">
			
		</ul>
	</div>

	<div class="book-summary">
		<div class="book-summary-title">文档目录</div>
		
		
		<nav role="navigation">
			


<ul class="summary">
    
    

    

    
        
        <li class="header">Part I</li>
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../C++必知必会的知识点/">
            
                <a href="../C++必知必会的知识点/">
            
                    
                    C++必知必会的知识点
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../C++必知必会的知识点/如何成为一名合格的CC++开发者？.html">
            
                <a href="../C++必知必会的知识点/如何成为一名合格的CC++开发者？.html">
            
                    
                    如何成为一名合格的C/C++开发者？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../C++必知必会的知识点/不定参数函数实现var_arg系列的宏.html">
            
                <a href="../C++必知必会的知识点/不定参数函数实现var_arg系列的宏.html">
            
                    
                    不定参数函数实现var_arg系列的宏
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../C++必知必会的知识点/你一定要搞明白的C函数调用方式与栈原理.html">
            
                <a href="../C++必知必会的知识点/你一定要搞明白的C函数调用方式与栈原理.html">
            
                    
                    你一定要搞明白的C函数调用方式与栈原理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../C++必知必会的知识点/深入理解CC++中的指针.html">
            
                <a href="../C++必知必会的知识点/深入理解CC++中的指针.html">
            
                    
                    深入理解C/C++中的指针
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="../C++必知必会的知识点/详解C++11中的智能指针.html">
            
                <a href="../C++必知必会的知识点/详解C++11中的智能指针.html">
            
                    
                    详解C++11中的智能指针
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.6" data-path="../C++必知必会的知识点/C++17结构化绑定.html">
            
                <a href="../C++必知必会的知识点/C++17结构化绑定.html">
            
                    
                    C++17结构化绑定
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.7" data-path="../C++必知必会的知识点/C++必须掌握的pimpl惯用法.html">
            
                <a href="../C++必知必会的知识点/C++必须掌握的pimpl惯用法.html">
            
                    
                    C++必须掌握的pimpl惯用法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.8" data-path="../C++必知必会的知识点/用VisualStudio调试Linux程序.html">
            
                <a href="../C++必知必会的知识点/用VisualStudio调试Linux程序.html">
            
                    
                    用Visual Studio调试Linux程序
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.9" data-path="../C++必知必会的知识点/如何使用VisualStudio管理和阅读开源项目代码.html">
            
                <a href="../C++必知必会的知识点/如何使用VisualStudio管理和阅读开源项目代码.html">
            
                    
                    如何使用Visual Studio管理和阅读开源项目代码
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.10" data-path="../C++必知必会的知识点/利用cmake工具生成VisualStudio工程文件.html">
            
                <a href="../C++必知必会的知识点/利用cmake工具生成VisualStudio工程文件.html">
            
                    
                    利用cmake工具生成Visual Studio工程文件
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../多线程/">
            
                <a href="../多线程/">
            
                    
                    多线程
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../多线程/后台C++开发你一定要知道的条件变量.html">
            
                <a href="../多线程/后台C++开发你一定要知道的条件变量.html">
            
                    
                    后台C++开发你一定要知道的条件变量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../多线程/整型变量赋值是原子操作吗？.html">
            
                <a href="../多线程/整型变量赋值是原子操作吗？.html">
            
                    
                    整型变量赋值是原子操作吗？
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="../网络编程/">
            
                <a href="../网络编程/">
            
                    
                    网络编程
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1.1" data-path="../网络编程/bind函数重难点解析.html">
            
                <a href="../网络编程/bind函数重难点解析.html">
            
                    
                    bind 函数重难点解析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.2" data-path="../网络编程/connect函数在阻塞和非阻塞模式下的行为.html">
            
                <a href="../网络编程/connect函数在阻塞和非阻塞模式下的行为.html">
            
                    
                    connect 函数在阻塞和非阻塞模式下的行为
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.3" data-path="../网络编程/select函数重难点解析.html">
            
                <a href="../网络编程/select函数重难点解析.html">
            
                    
                    select 函数重难点解析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.4" data-path="../网络编程/Linuxepoll模型（含LT模式和ET模式详解）.html">
            
                <a href="../网络编程/Linuxepoll模型（含LT模式和ET模式详解）.html">
            
                    
                    Linux epoll 模型（含LT 模式和 ET 模式详解）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.5" data-path="../网络编程/socket的阻塞模式和非阻塞模式.html">
            
                <a href="../网络编程/socket的阻塞模式和非阻塞模式.html">
            
                    
                    socket 的阻塞模式和非阻塞模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.6" data-path="../网络编程/非阻塞模式下send和recv函数的返回值.html">
            
                <a href="../网络编程/非阻塞模式下send和recv函数的返回值.html">
            
                    
                    非阻塞模式下 send 和 recv 函数的返回值
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.7" data-path="../网络编程/服务器开发通信协议设计介绍.html">
            
                <a href="../网络编程/服务器开发通信协议设计介绍.html">
            
                    
                    服务器开发通信协议设计介绍
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.8" data-path="../网络编程/TCP协议如何解决粘包、半包问题.html">
            
                <a href="../网络编程/TCP协议如何解决粘包、半包问题.html">
            
                    
                    TCP 协议如何解决粘包、半包问题
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.9" data-path="../网络编程/网络通信中收发数据的正确姿势.html">
            
                <a href="../网络编程/网络通信中收发数据的正确姿势.html">
            
                    
                    网络通信中收发数据的正确姿势
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.10" data-path="../网络编程/服务器端发数据时，如果对端一直不收，怎么办？.html">
            
                <a href="../网络编程/服务器端发数据时，如果对端一直不收，怎么办？.html">
            
                    
                    服务器端发数据时，如果对端一直不收，怎么办？
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="../程序员必知必会的网络命令/">
            
                <a href="../程序员必知必会的网络命令/">
            
                    
                    程序员必知必会的网络命令
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1.1" data-path="../程序员必知必会的网络命令/利用telnet命令发电子邮件.html">
            
                <a href="../程序员必知必会的网络命令/利用telnet命令发电子邮件.html">
            
                    
                    利用telnet命令发电子邮件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.2" data-path="../程序员必知必会的网络命令/做Java或者C++开发都应该知道的lsof命令.html">
            
                <a href="../程序员必知必会的网络命令/做Java或者C++开发都应该知道的lsof命令.html">
            
                    
                    做Java或者C++开发都应该知道的lsof命令
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.3" data-path="../程序员必知必会的网络命令/Linux网络故障排查的瑞士军刀.html">
            
                <a href="../程序员必知必会的网络命令/Linux网络故障排查的瑞士军刀.html">
            
                    
                    Linux网络故障排查的瑞士军刀nc命令
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.4" data-path="../程序员必知必会的网络命令/Linuxtcpdump使用介绍.html">
            
                <a href="../程序员必知必会的网络命令/Linuxtcpdump使用介绍.html">
            
                    
                    Linux tcpdump使用详解
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.5" data-path="../程序员必知必会的网络命令/从抓包的角度分析connect函数的连接过程.html">
            
                <a href="../程序员必知必会的网络命令/从抓包的角度分析connect函数的连接过程.html">
            
                    
                    从抓包的角度分析connect函数的连接过程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.6" data-path="../程序员必知必会的网络命令/服务器开发中网络数据分析与故障排查经验漫谈.html">
            
                <a href="../程序员必知必会的网络命令/服务器开发中网络数据分析与故障排查经验漫谈.html">
            
                    
                    服务器开发中网络数据分析与故障排查经验漫谈
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="header">Part II</li>
        
        
    
        <li class="chapter " data-level="4.1" data-path="../高性能服务器框架设计/">
            
                <a href="../高性能服务器框架设计/">
            
                    
                    高性能服务器框架设计
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1.1" data-path="../高性能服务器框架设计/主线程与工作线程的分工.html">
            
                <a href="../高性能服务器框架设计/主线程与工作线程的分工.html">
            
                    
                    主线程与工作线程的分工
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.2" data-path="../高性能服务器框架设计/Reactor模式.html">
            
                <a href="../高性能服务器框架设计/Reactor模式.html">
            
                    
                    Reactor模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.3" data-path="../高性能服务器框架设计/实例：一个服务器程序的架构介绍.html">
            
                <a href="../高性能服务器框架设计/实例：一个服务器程序的架构介绍.html">
            
                    
                    实例：一个服务器程序的架构介绍
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.4" data-path="../高性能服务器框架设计/错误码系统的设计.html">
            
                <a href="../高性能服务器框架设计/错误码系统的设计.html">
            
                    
                    错误码系统的设计
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.5" data-path="../高性能服务器框架设计/日志系统的设计.html">
            
                <a href="../高性能服务器框架设计/日志系统的设计.html">
            
                    
                    日志系统的设计
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.6" data-path="../高性能服务器框架设计/如何设计断线自动重连机制.html">
            
                <a href="../高性能服务器框架设计/如何设计断线自动重连机制.html">
            
                    
                    如何设计断线自动重连机制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.7" data-path="../高性能服务器框架设计/心跳包机制设计详解.html">
            
                <a href="../高性能服务器框架设计/心跳包机制设计详解.html">
            
                    
                    心跳包机制设计详解
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.8" data-path="../高性能服务器框架设计/业务数据处理一定要单独开线程吗.html">
            
                <a href="../高性能服务器框架设计/业务数据处理一定要单独开线程吗.html">
            
                    
                    业务数据处理一定要单独开线程吗
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.9" data-path="../高性能服务器框架设计/C++高性能服务器网络框架设计细节.html">
            
                <a href="../高性能服务器框架设计/C++高性能服务器网络框架设计细节.html">
            
                    
                    C++ 高性能服务器网络框架设计细节
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="5.1" data-path="../服务器开发案例实战/">
            
                <a href="../服务器开发案例实战/">
            
                    
                    服务器开发案例实战
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1.1" data-path="../服务器开发案例实战/从零实现一个http服务器.html">
            
                <a href="../服务器开发案例实战/从零实现一个http服务器.html">
            
                    
                    从零实现一个http服务器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.1.2" data-path="../服务器开发案例实战/从零实现一款12306刷票软件.html">
            
                <a href="../服务器开发案例实战/从零实现一款12306刷票软件.html">
            
                    
                    从零实现一款12306刷票软件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.1.3" data-path="../服务器开发案例实战/从零实现一个邮件收发客户端.html">
            
                <a href="../服务器开发案例实战/从零实现一个邮件收发客户端.html">
            
                    
                    从零实现一个邮件收发客户端
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.1.4" data-path="../服务器开发案例实战/从零开发一个WebSocket服务器.html">
            
                <a href="../服务器开发案例实战/从零开发一个WebSocket服务器.html">
            
                    
                    从零开发一个WebSocket服务器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.1.5" data-path="../服务器开发案例实战/1从一款多人联机实时对战游戏开始.html">
            
                <a href="../服务器开发案例实战/1从一款多人联机实时对战游戏开始.html">
            
                    
                    从零学习开源项目系列（一） 从一款多人联机实时对战游戏开始
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.1.6" data-path="../服务器开发案例实战/2最后一战概况.html">
            
                <a href="../服务器开发案例实战/2最后一战概况.html">
            
                    
                    从零学习开源项目系列（二） 最后一战概况
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.1.7" data-path="../服务器开发案例实战/3CSBattleMgr服务源码研究.html">
            
                <a href="../服务器开发案例实战/3CSBattleMgr服务源码研究.html">
            
                    
                    从零学习开源项目系列（三） CSBattleMgr服务源码研究
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.1.8" data-path="../服务器开发案例实战/4LogServer源码探究.html">
            
                <a href="../服务器开发案例实战/4LogServer源码探究.html">
            
                    
                    从零学习开源项目系列（四）LogServer源码探究
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="header">Part III</li>
        
        
    
        <li class="chapter " data-level="6.1" data-path="./">
            
                <a href="./">
            
                    
                    TeamTalk IM源码分析
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.1.1" data-path="01TeamTalk介绍.html">
            
                <a href="01TeamTalk介绍.html">
            
                    
                    01 TeamTalk介绍
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.1.2" data-path="02服务器端的程序的编译与部署.html">
            
                <a href="02服务器端的程序的编译与部署.html">
            
                    
                    02 服务器端的程序的编译与部署
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.1.3" data-path="03服务器端的程序架构介绍.html">
            
                <a href="03服务器端的程序架构介绍.html">
            
                    
                    03 服务器端的程序架构介绍
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="6.1.4" data-path="04服务器端db_proxy_server源码分析.html">
            
                <a href="04服务器端db_proxy_server源码分析.html">
            
                    
                    04 服务器端db_proxy_server源码分析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.1.5" data-path="05服务器端msg_server源码分析.html">
            
                <a href="05服务器端msg_server源码分析.html">
            
                    
                    05 服务器端msg_server源码分析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.1.6" data-path="06服务器端login_server源码分析.html">
            
                <a href="06服务器端login_server源码分析.html">
            
                    
                    06 服务器端login_server源码分析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.1.7" data-path="07服务器端msfs源码分析.html">
            
                <a href="07服务器端msfs源码分析.html">
            
                    
                    07 服务器端msfs源码分析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.1.8" data-path="08服务器端file_server源码分析.html">
            
                <a href="08服务器端file_server源码分析.html">
            
                    
                    08 服务器端file_server源码分析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.1.9" data-path="09服务器端route_server源码分析.html">
            
                <a href="09服务器端route_server源码分析.html">
            
                    
                    09 服务器端route_server源码分析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.1.10" data-path="10开放一个TeamTalk测试服务器地址和几个测试账号.html">
            
                <a href="10开放一个TeamTalk测试服务器地址和几个测试账号.html">
            
                    
                    10 开放一个TeamTalk测试服务器地址和几个测试账号
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.1.11" data-path="11pc客户端源码分析.html">
            
                <a href="11pc客户端源码分析.html">
            
                    
                    11 pc客户端源码分析
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="7.1" data-path="../libevent源码深度剖析/">
            
                <a href="../libevent源码深度剖析/">
            
                    
                    libevent源码深度剖析
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="7.1.1" data-path="../libevent源码深度剖析/libevent源码深度剖析01.html">
            
                <a href="../libevent源码深度剖析/libevent源码深度剖析01.html">
            
                    
                    libevent源码深度剖析01
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.1.2" data-path="../libevent源码深度剖析/libevent源码深度剖析02.html">
            
                <a href="../libevent源码深度剖析/libevent源码深度剖析02.html">
            
                    
                    libevent源码深度剖析02
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.1.3" data-path="../libevent源码深度剖析/libevent源码深度剖析03.html">
            
                <a href="../libevent源码深度剖析/libevent源码深度剖析03.html">
            
                    
                    libevent源码深度剖析03
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.1.4" data-path="../libevent源码深度剖析/libevent源码深度剖析04.html">
            
                <a href="../libevent源码深度剖析/libevent源码深度剖析04.html">
            
                    
                    libevent源码深度剖析04
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.1.5" data-path="../libevent源码深度剖析/libevent源码深度剖析05.html">
            
                <a href="../libevent源码深度剖析/libevent源码深度剖析05.html">
            
                    
                    libevent源码深度剖析05
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.1.6" data-path="../libevent源码深度剖析/libevent源码深度剖析06.html">
            
                <a href="../libevent源码深度剖析/libevent源码深度剖析06.html">
            
                    
                    libevent源码深度剖析06
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.1.7" data-path="../libevent源码深度剖析/libevent源码深度剖析07.html">
            
                <a href="../libevent源码深度剖析/libevent源码深度剖析07.html">
            
                    
                    libevent源码深度剖析07
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.1.8" data-path="../libevent源码深度剖析/libevent源码深度剖析08.html">
            
                <a href="../libevent源码深度剖析/libevent源码深度剖析08.html">
            
                    
                    libevent源码深度剖析08
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.1.9" data-path="../libevent源码深度剖析/libevent源码深度剖析09.html">
            
                <a href="../libevent源码深度剖析/libevent源码深度剖析09.html">
            
                    
                    libevent源码深度剖析09
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.1.10" data-path="../libevent源码深度剖析/libevent源码深度剖析10.html">
            
                <a href="../libevent源码深度剖析/libevent源码深度剖析10.html">
            
                    
                    libevent源码深度剖析10
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.1.11" data-path="../libevent源码深度剖析/libevent源码深度剖析11.html">
            
                <a href="../libevent源码深度剖析/libevent源码深度剖析11.html">
            
                    
                    libevent源码深度剖析11
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.1.12" data-path="../libevent源码深度剖析/libevent源码深度剖析12.html">
            
                <a href="../libevent源码深度剖析/libevent源码深度剖析12.html">
            
                    
                    libevent源码深度剖析12
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.1.13" data-path="../libevent源码深度剖析/libevent源码深度剖析13.html">
            
                <a href="../libevent源码深度剖析/libevent源码深度剖析13.html">
            
                    
                    libevent源码深度剖析13
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="7.2" data-path="../leveldb源码分析/">
            
                <a href="../leveldb源码分析/">
            
                    
                    leveldb源码分析
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="7.2.1" data-path="../leveldb源码分析/leveldb源码分析1.html">
            
                <a href="../leveldb源码分析/leveldb源码分析1.html">
            
                    
                    leveldb源码分析1
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.2" data-path="../leveldb源码分析/leveldb源码分析2.html">
            
                <a href="../leveldb源码分析/leveldb源码分析2.html">
            
                    
                    leveldb源码分析2
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.3" data-path="../leveldb源码分析/leveldb源码分析3.html">
            
                <a href="../leveldb源码分析/leveldb源码分析3.html">
            
                    
                    leveldb源码分析3
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.4" data-path="../leveldb源码分析/leveldb源码分析4.html">
            
                <a href="../leveldb源码分析/leveldb源码分析4.html">
            
                    
                    leveldb源码分析4
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.5" data-path="../leveldb源码分析/leveldb源码分析5.html">
            
                <a href="../leveldb源码分析/leveldb源码分析5.html">
            
                    
                    leveldb源码分析5
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.6" data-path="../leveldb源码分析/leveldb源码分析6.html">
            
                <a href="../leveldb源码分析/leveldb源码分析6.html">
            
                    
                    leveldb源码分析6
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.7" data-path="../leveldb源码分析/leveldb源码分析7.html">
            
                <a href="../leveldb源码分析/leveldb源码分析7.html">
            
                    
                    leveldb源码分析7
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.8" data-path="../leveldb源码分析/leveldb源码分析8.html">
            
                <a href="../leveldb源码分析/leveldb源码分析8.html">
            
                    
                    leveldb源码分析8
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.9" data-path="../leveldb源码分析/leveldb源码分析9.html">
            
                <a href="../leveldb源码分析/leveldb源码分析9.html">
            
                    
                    leveldb源码分析9
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.10" data-path="../leveldb源码分析/leveldb源码分析10.html">
            
                <a href="../leveldb源码分析/leveldb源码分析10.html">
            
                    
                    leveldb源码分析10
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.11" data-path="../leveldb源码分析/leveldb源码分析11.html">
            
                <a href="../leveldb源码分析/leveldb源码分析11.html">
            
                    
                    leveldb源码分析11
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.12" data-path="../leveldb源码分析/leveldb源码分析12.html">
            
                <a href="../leveldb源码分析/leveldb源码分析12.html">
            
                    
                    leveldb源码分析12
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.13" data-path="../leveldb源码分析/leveldb源码分析13.html">
            
                <a href="../leveldb源码分析/leveldb源码分析13.html">
            
                    
                    leveldb源码分析13
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.14" data-path="../leveldb源码分析/leveldb源码分析14.html">
            
                <a href="../leveldb源码分析/leveldb源码分析14.html">
            
                    
                    leveldb源码分析14
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.15" data-path="../leveldb源码分析/leveldb源码分析15.html">
            
                <a href="../leveldb源码分析/leveldb源码分析15.html">
            
                    
                    leveldb源码分析15
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.16" data-path="../leveldb源码分析/leveldb源码分析16.html">
            
                <a href="../leveldb源码分析/leveldb源码分析16.html">
            
                    
                    leveldb源码分析16
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.17" data-path="../leveldb源码分析/leveldb源码分析17.html">
            
                <a href="../leveldb源码分析/leveldb源码分析17.html">
            
                    
                    leveldb源码分析17
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.18" data-path="../leveldb源码分析/leveldb源码分析18.html">
            
                <a href="../leveldb源码分析/leveldb源码分析18.html">
            
                    
                    leveldb源码分析18
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.19" data-path="../leveldb源码分析/leveldb源码分析19.html">
            
                <a href="../leveldb源码分析/leveldb源码分析19.html">
            
                    
                    leveldb源码分析19
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.20" data-path="../leveldb源码分析/leveldb源码分析20.html">
            
                <a href="../leveldb源码分析/leveldb源码分析20.html">
            
                    
                    leveldb源码分析20
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.21" data-path="../leveldb源码分析/leveldb源码分析21.html">
            
                <a href="../leveldb源码分析/leveldb源码分析21.html">
            
                    
                    leveldb源码分析21
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.22" data-path="../leveldb源码分析/leveldb源码分析22.html">
            
                <a href="../leveldb源码分析/leveldb源码分析22.html">
            
                    
                    leveldb源码分析22
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="8.1" data-path="../Memcached源码分析/">
            
                <a href="../Memcached源码分析/">
            
                    
                    Memcached源码分析
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="8.1.1" data-path="../Memcached源码分析/00服务器资源调整.html">
            
                <a href="../Memcached源码分析/00服务器资源调整.html">
            
                    
                    00 服务器资源调整
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.1.2" data-path="../Memcached源码分析/01初始化参数解析.html">
            
                <a href="../Memcached源码分析/01初始化参数解析.html">
            
                    
                    01 初始化参数解析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.1.3" data-path="../Memcached源码分析/02网络监听的建立.html">
            
                <a href="../Memcached源码分析/02网络监听的建立.html">
            
                    
                    02 网络监听的建立
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.1.4" data-path="../Memcached源码分析/03网络连接建立.html">
            
                <a href="../Memcached源码分析/03网络连接建立.html">
            
                    
                    03 网络连接建立
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.1.5" data-path="../Memcached源码分析/04内存初始化.html">
            
                <a href="../Memcached源码分析/04内存初始化.html">
            
                    
                    04 内存初始化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.1.6" data-path="../Memcached源码分析/05资源初始化.html">
            
                <a href="../Memcached源码分析/05资源初始化.html">
            
                    
                    05 资源初始化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.1.7" data-path="../Memcached源码分析/06get过程.html">
            
                <a href="../Memcached源码分析/06get过程.html">
            
                    
                    06 get过程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.1.8" data-path="../Memcached源码分析/07cas属性.html">
            
                <a href="../Memcached源码分析/07cas属性.html">
            
                    
                    07 cas属性
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.1.9" data-path="../Memcached源码分析/08内存池.html">
            
                <a href="../Memcached源码分析/08内存池.html">
            
                    
                    08 内存池
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.1.10" data-path="../Memcached源码分析/09连接队列.html">
            
                <a href="../Memcached源码分析/09连接队列.html">
            
                    
                    09 连接队列
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.1.11" data-path="../Memcached源码分析/10Hash表操作.html">
            
                <a href="../Memcached源码分析/10Hash表操作.html">
            
                    
                    10 Hash表操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.1.12" data-path="../Memcached源码分析/12set操作.html">
            
                <a href="../Memcached源码分析/12set操作.html">
            
                    
                    12 set操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.1.13" data-path="../Memcached源码分析/13do_item_alloc操作.html">
            
                <a href="../Memcached源码分析/13do_item_alloc操作.html">
            
                    
                    13 do_item_alloc操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.1.14" data-path="../Memcached源码分析/14item结构.html">
            
                <a href="../Memcached源码分析/14item结构.html">
            
                    
                    14 item结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.1.15" data-path="../Memcached源码分析/15Hash表扩容.html">
            
                <a href="../Memcached源码分析/15Hash表扩容.html">
            
                    
                    15 Hash表扩容
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.1.16" data-path="../Memcached源码分析/16线程交互.html">
            
                <a href="../Memcached源码分析/16线程交互.html">
            
                    
                    16 线程交互
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.1.17" data-path="../Memcached源码分析/17状态机.html">
            
                <a href="../Memcached源码分析/17状态机.html">
            
                    
                    17 状态机
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="9.1" data-path="../游戏开发专题/">
            
                <a href="../游戏开发专题/">
            
                    
                    游戏开发专题
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="9.1.1" data-path="../游戏开发专题/1游戏服务器开发的基本体系与服务器端开发的一些建议.html">
            
                <a href="../游戏开发专题/1游戏服务器开发的基本体系与服务器端开发的一些建议.html">
            
                    
                    1 游戏服务器开发的基本体系与服务器端开发的一些建议
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="9.1.2" data-path="../游戏开发专题/2网络游戏服务器开发框架设计介绍.html">
            
                <a href="../游戏开发专题/2网络游戏服务器开发框架设计介绍.html">
            
                    
                    2 网络游戏服务器开发框架设计介绍
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="9.1.3" data-path="../游戏开发专题/3游戏后端开发需要掌握的知识.html">
            
                <a href="../游戏开发专题/3游戏后端开发需要掌握的知识.html">
            
                    
                    3 游戏后端开发需要掌握的知识
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="9.1.4" data-path="../游戏开发专题/4关于游戏服务端架构的整理.html">
            
                <a href="../游戏开发专题/4关于游戏服务端架构的整理.html">
            
                    
                    4 关于游戏服务端架构的整理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="9.1.5" data-path="../游戏开发专题/5各类游戏对应的服务端架构.html">
            
                <a href="../游戏开发专题/5各类游戏对应的服务端架构.html">
            
                    
                    5 各类游戏对应的服务端架构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="9.1.6" data-path="../游戏开发专题/6从腾讯QQgame高性能服务器集群架构看“分而治之”与“自治”等分布式架构设计原则.html">
            
                <a href="../游戏开发专题/6从腾讯QQgame高性能服务器集群架构看“分而治之”与“自治”等分布式架构设计原则.html">
            
                    
                    6 从腾讯QQgame高性能服务器集群架构看“分而治之”与“自治”等分布式架构设计原则
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="9.1.7" data-path="../游戏开发专题/7QQ游戏百万人同时在线服务器架构实现.html">
            
                <a href="../游戏开发专题/7QQ游戏百万人同时在线服务器架构实现.html">
            
                    
                    7 QQ游戏百万人同时在线服务器架构实现
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="9.1.8" data-path="../游戏开发专题/8大型多人在线游戏服务器架构设计.html">
            
                <a href="../游戏开发专题/8大型多人在线游戏服务器架构设计.html">
            
                    
                    8 大型多人在线游戏服务器架构设计
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="9.1.9" data-path="../游戏开发专题/9百万用户级游戏服务器架构设计.html">
            
                <a href="../游戏开发专题/9百万用户级游戏服务器架构设计.html">
            
                    
                    9 百万用户级游戏服务器架构设计
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="9.1.10" data-path="../游戏开发专题/10十万在线的WebGame的数据库设计思路.html">
            
                <a href="../游戏开发专题/10十万在线的WebGame的数据库设计思路.html">
            
                    
                    10 十万在线的WebGame的数据库设计思路
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="9.1.11" data-path="../游戏开发专题/11一种高性能网络游戏服务器架构设计.html">
            
                <a href="../游戏开发专题/11一种高性能网络游戏服务器架构设计.html">
            
                    
                    11 一种高性能网络游戏服务器架构设计
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="9.1.12" data-path="../游戏开发专题/12经典游戏服务器端架构概述.html">
            
                <a href="../游戏开发专题/12经典游戏服务器端架构概述.html">
            
                    
                    12 经典游戏服务器端架构概述
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="9.1.13" data-path="../游戏开发专题/13游戏跨服架构进化之路.html">
            
                <a href="../游戏开发专题/13游戏跨服架构进化之路.html">
            
                    
                    13 游戏跨服架构进化之路
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="header">Part IV</li>
        
        
    
        <li class="chapter " data-level="10.1" data-path="../程序员面试题精讲/">
            
                <a href="../程序员面试题精讲/">
            
                    
                    程序员面试题精讲
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="10.1.1" data-path="../程序员面试题精讲/腾讯后台开发实习生技能要求.html">
            
                <a href="../程序员面试题精讲/腾讯后台开发实习生技能要求.html">
            
                    
                    腾讯后台开发实习生技能要求
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="10.1.2" data-path="../程序员面试题精讲/聊聊如何拿大厂的offer.html">
            
                <a href="../程序员面试题精讲/聊聊如何拿大厂的offer.html">
            
                    
                    聊聊如何拿大厂的 offer
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="10.1.3" data-path="../程序员面试题精讲/网络通信题目集锦.html">
            
                <a href="../程序员面试题精讲/网络通信题目集锦.html">
            
                    
                    网络通信题目集锦
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="10.1.4" data-path="../程序员面试题精讲/我面试后端开发经理的经历.html">
            
                <a href="../程序员面试题精讲/我面试后端开发经理的经历.html">
            
                    
                    我面试后端开发经理的经历
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="10.1.5" data-path="../程序员面试题精讲/LinuxCC++后端开发面试问哪些问题.html">
            
                <a href="../程序员面试题精讲/LinuxCC++后端开发面试问哪些问题.html">
            
                    
                    Linux C/C++后端开发面试问哪些问题
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="10.2" data-path="../职业规划/">
            
                <a href="../职业规划/">
            
                    
                    职业规划
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="10.2.1" data-path="../职业规划/给工作4年迷茫的程序员们的一点建议.html">
            
                <a href="../职业规划/给工作4年迷茫的程序员们的一点建议.html">
            
                    
                    给工作 4 年迷茫的程序员们的一点建议
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="10.2.2" data-path="../职业规划/聊聊技术人员的常见的职业问题.html">
            
                <a href="../职业规划/聊聊技术人员的常见的职业问题.html">
            
                    
                    聊聊技术人员的常见的职业问题
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="10.2.3" data-path="../职业规划/写给那些傻傻想做服务器开发的朋友.html">
            
                <a href="../职业规划/写给那些傻傻想做服务器开发的朋友.html">
            
                    
                    写给那些傻傻想做服务器开发的朋友
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="10.3" data-path="../自我提升与开源代码/">
            
                <a href="../自我提升与开源代码/">
            
                    
                    自我提升与开源代码
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="10.3.1" data-path="../自我提升与开源代码/2020年好好读一读开源代码吧.html">
            
                <a href="../自我提升与开源代码/2020年好好读一读开源代码吧.html">
            
                    
                    2020 年好好读一读开源代码吧
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="10.4" data-path="../后端开发相关的书籍/">
            
                <a href="../后端开发相关的书籍/">
            
                    
                    后端开发相关的书籍
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="10.4.1" data-path="../后端开发相关的书籍/后台开发应该读的书.html">
            
                <a href="../后端开发相关的书籍/后台开发应该读的书.html">
            
                    
                    后台开发应该读的书
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="10.5" data-path="../程序员的简历/">
            
                <a href="../程序员的简历/">
            
                    
                    程序员的简历
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="10.5.1" data-path="../程序员的简历/程序员如何写简历.html">
            
                <a href="../程序员的简历/程序员如何写简历.html">
            
                    
                    程序员如何写简历
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="10.6" data-path="../程序员的薪资与年终奖那些事儿/">
            
                <a href="../程序员的薪资与年终奖那些事儿/">
            
                    
                    程序员的薪资与年终奖那些事儿
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="10.6.1" data-path="../程序员的薪资与年终奖那些事儿/技术面试与HR谈薪资技巧.html">
            
                <a href="../程序员的薪资与年终奖那些事儿/技术面试与HR谈薪资技巧.html">
            
                    
                    技术面试与HR谈薪资技巧
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="10.6.2" data-path="../程序员的薪资与年终奖那些事儿/聊一聊程序员如何增加收入.html">
            
                <a href="../程序员的薪资与年终奖那些事儿/聊一聊程序员如何增加收入.html">
            
                    
                    聊一聊程序员如何增加收入
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="10.6.3" data-path="../程序员的薪资与年终奖那些事儿/谈一谈年终奖.html">
            
                <a href="../程序员的薪资与年终奖那些事儿/谈一谈年终奖.html">
            
                    
                    谈一谈年终奖
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="10.7" data-path="../程序员的烦心事/">
            
                <a href="../程序员的烦心事/">
            
                    
                    程序员的烦心事
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="10.7.1" data-path="../程序员的烦心事/拒绝了一家公司的offer后，他们的副总和hr总监同时打电话来询问拒绝原因并极力要求加入，我该不该去？.html">
            
                <a href="../程序员的烦心事/拒绝了一家公司的offer后，他们的副总和hr总监同时打电话来询问拒绝原因并极力要求加入，我该不该去？.html">
            
                    
                    拒绝了一家公司的offer后，他们的副总和hr总监同时打电话来询问拒绝原因并极力要求加入，我该不该去？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="10.7.2" data-path="../程序员的烦心事/我是一名程序员，结婚时女友要求我用两年的工资作为彩礼，我该不该答应？.html">
            
                <a href="../程序员的烦心事/我是一名程序员，结婚时女友要求我用两年的工资作为彩礼，我该不该答应？.html">
            
                    
                    我是一名程序员，结婚时女友要求我用两年的工资作为彩礼，我该不该答应？
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="10.8" data-path="../作者的故事/">
            
                <a href="../作者的故事/">
            
                    
                    作者的故事
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="10.8.1" data-path="../作者的故事/我的2019.html">
            
                <a href="../作者的故事/我的2019.html">
            
                    
                    我的 2019
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="10.8.2" data-path="../作者的故事/我是如何年薪五十万的.html">
            
                <a href="../作者的故事/我是如何年薪五十万的.html">
            
                    
                    我是如何年薪五十万的
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


		</nav>
		
		
	</div>

	<!-- Content nav -->
	<div class="book-anchor">
		<div class="book-anchor-title">在这篇文章中:</div>
		<div class="book-anchor-body">

		</div>
	</div>

	<div class="book-body">
		
		<div class="body-inner">
			
			

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >04 服务器端db_proxy_server源码分析</a>
    </h1>
</div>




			<div class="page-wrapper" tabindex="-1" role="main">
				<div class="page-inner">
					
<div id="book-search-results">
    <div class="search-noresults">
    
					<section class="normal markdown-section">
						
						<div id="anchor-navigation-ex-navbar"><i class="fa fa-navicon"></i><ul><li><span class="title-icon "></span><a href="#04-&#x670D;&#x52A1;&#x5668;&#x7AEF;dbproxyserver&#x6E90;&#x7801;&#x5206;&#x6790;"><b></b>04 &#x670D;&#x52A1;&#x5668;&#x7AEF;db_proxy_server&#x6E90;&#x7801;&#x5206;&#x6790;</a></li><ul><ul><li><span class="title-icon "></span><a href="#&#x4E00;&#x3001;&#x521D;&#x59CB;&#x5316;redis&#x8FDE;&#x63A5;"><b></b>&#x4E00;&#x3001;&#x521D;&#x59CB;&#x5316;redis&#x8FDE;&#x63A5;</a></li><li><span class="title-icon "></span><a href="#&#x4E8C;&#x3001;&#x521D;&#x59CB;&#x5316;mysql&#x8FDE;&#x63A5;"><b></b>&#x4E8C;&#x3001;&#x521D;&#x59CB;&#x5316;mysql&#x8FDE;&#x63A5;</a></li><li><span class="title-icon "></span><a href="#&#x4E09;&#x3001;&#x542F;&#x52A8;&#x4EFB;&#x52A1;&#x961F;&#x5217;&#xFF0C;&#x7528;&#x4E8E;&#x5904;&#x7406;&#x4EFB;&#x52A1;"><b></b>&#x4E09;&#x3001;&#x542F;&#x52A8;&#x4EFB;&#x52A1;&#x961F;&#x5217;&#xFF0C;&#x7528;&#x4E8E;&#x5904;&#x7406;&#x4EFB;&#x52A1;</a></li><li><span class="title-icon "></span><a href="#&#x56DB;&#x3001;&#x542F;&#x52A8;&#x4ECE;mysql&#x540C;&#x6B65;&#x6570;&#x636E;&#x5230;redis&#x5DE5;&#x4F5C;"><b></b>&#x56DB;&#x3001;&#x542F;&#x52A8;&#x4ECE;mysql&#x540C;&#x6B65;&#x6570;&#x636E;&#x5230;redis&#x5DE5;&#x4F5C;</a></li><li><span class="title-icon "></span><a href="#&#x4E94;&#x3001;&#x5728;&#x7AEF;&#x53E3;10600&#x4E0A;&#x542F;&#x52A8;&#x4FA6;&#x542C;&#xFF0C;&#x76D1;&#x542C;&#x65B0;&#x8FDE;&#x63A5;"><b></b>&#x4E94;&#x3001;&#x5728;&#x7AEF;&#x53E3;10600&#x4E0A;&#x542F;&#x52A8;&#x4FA6;&#x542C;&#xFF0C;&#x76D1;&#x542C;&#x65B0;&#x8FDE;&#x63A5;</a></li><li><span class="title-icon "></span><a href="#&#x516D;&#x3001;&#x4E3B;&#x7EBF;&#x7A0B;&#x8FDB;&#x5165;&#x5FAA;&#x73AF;&#xFF0C;&#x76D1;&#x542C;&#x65B0;&#x8FDE;&#x63A5;&#x7684;&#x5230;&#x6765;&#x4EE5;&#x53CA;&#x51FA;&#x6765;&#x65B0;&#x8FDE;&#x63A5;&#x4E0A;&#x7684;&#x6570;&#x636E;&#x6536;&#x53D1;"><b></b>&#x516D;&#x3001;&#x4E3B;&#x7EBF;&#x7A0B;&#x8FDB;&#x5165;&#x5FAA;&#x73AF;&#xFF0C;&#x76D1;&#x542C;&#x65B0;&#x8FDE;&#x63A5;&#x7684;&#x5230;&#x6765;&#x4EE5;&#x53CA;&#x51FA;&#x6765;&#x65B0;&#x8FDE;&#x63A5;&#x4E0A;&#x7684;&#x6570;&#x636E;&#x6536;&#x53D1;</a></li></ul></ul></ul></div><h1 id="04-&#x670D;&#x52A1;&#x5668;&#x7AEF;dbproxyserver&#x6E90;&#x7801;&#x5206;&#x6790;"><a name="04-&#x670D;&#x52A1;&#x5668;&#x7AEF;dbproxyserver&#x6E90;&#x7801;&#x5206;&#x6790;" class="anchor-navigation-ex-anchor" href="#04-&#x670D;&#x52A1;&#x5668;&#x7AEF;dbproxyserver&#x6E90;&#x7801;&#x5206;&#x6790;"><i class="fa fa-link" aria-hidden="true"></i></a>04 &#x670D;&#x52A1;&#x5668;&#x7AEF;db_proxy_server&#x6E90;&#x7801;&#x5206;&#x6790;</h1>
<p>&#x4ECE;&#x8FD9;&#x7BC7;&#x6587;&#x7AE0;&#x5F00;&#x59CB;&#xFF0C;&#x6211;&#x5C06;&#x8BE6;&#x7EC6;&#x5730;&#x5206;&#x6790;TeamTalk&#x670D;&#x52A1;&#x5668;&#x7AEF;&#x6BCF;&#x4E00;&#x4E2A;&#x670D;&#x52A1;&#x7684;&#x6E90;&#x7801;&#x548C;&#x67B6;&#x6784;&#x8BBE;&#x8BA1;&#x3002;</p>
<p>&#x8FD9;&#x7BC7;&#x4ECE;db_proxy_server&#x5F00;&#x59CB;&#x3002;db_proxy_server&#x662F;TeamTalk&#x670D;&#x52A1;&#x5668;&#x7AEF;&#x6700;&#x540E;&#x7AEF;&#x7684;&#x7A0B;&#x5E8F;&#xFF0C;&#x5B83;&#x8FDE;&#x63A5;&#x7740;&#x5173;&#x7CFB;&#x578B;&#x6570;&#x636E;&#x5E93;mysql&#x548C;nosql&#x5185;&#x5B58;&#x6570;&#x636E;&#x5E93;redis&#x3002;&#x5176;&#x4F4D;&#x7F6E;&#x5728;&#x6574;&#x4E2A;&#x670D;&#x52A1;&#x67B6;&#x6784;&#x4E2D;&#x5982;&#x56FE;&#x6240;&#x793A;&#xFF1A;</p>
<p><img src="../imgs/tt11.png" alt=""><img src="" alt="&#x70B9;&#x51FB;&#x5E76;&#x62D6;&#x62FD;&#x4EE5;&#x79FB;&#x52A8;"></p>
<p>&#x6211;&#x4EEC;&#x4ECE;db_proxy_server&#x7684;main()&#x51FD;&#x6570;&#x5F00;&#x59CB;&#xFF0C;main()&#x51FD;&#x6570;&#x5176;&#x5B9E;&#x5C31;&#x662F;&#x505A;&#x4E86;&#x4EE5;&#x4E0B;&#x521D;&#x59CB;&#x5316;&#x5DE5;&#x4F5C;&#xFF0C;&#x6211;&#x6574;&#x7406;&#x6210;&#x5982;&#x4E0B;&#x4F2A;&#x7801;&#xFF1A;</p>
<pre class="language-"><code class="lang-plain">int main()
{
    //1. &#x521D;&#x59CB;&#x5316;redis&#x8FDE;&#x63A5;

    //2. &#x521D;&#x59CB;&#x5316;mysql&#x8FDE;&#x63A5;

    //3. &#x542F;&#x52A8;&#x4EFB;&#x52A1;&#x961F;&#x5217;&#xFF0C;&#x7528;&#x4E8E;&#x5904;&#x7406;&#x4EFB;&#x52A1;

    //4. &#x542F;&#x52A8;&#x4ECE;mysql&#x540C;&#x6B65;&#x6570;&#x636E;&#x5230;redis&#x5DE5;&#x4F5C;

    //5. &#x5728;&#x7AEF;&#x53E3;10600&#x4E0A;&#x542F;&#x52A8;&#x4FA6;&#x542C;&#xFF0C;&#x76D1;&#x542C;&#x65B0;&#x8FDE;&#x63A5;

    //6. &#x4E3B;&#x7EBF;&#x7A0B;&#x8FDB;&#x5165;&#x5FAA;&#x73AF;&#xFF0C;&#x76D1;&#x542C;&#x65B0;&#x8FDE;&#x63A5;&#x7684;&#x5230;&#x6765;&#x4EE5;&#x53CA;&#x51FA;&#x6765;&#x65B0;&#x8FDE;&#x63A5;&#x4E0A;&#x7684;&#x6570;&#x636E;&#x6536;&#x53D1;
}
</code></pre>
<p>&#x4E0B;&#x9762;&#xFF0C;&#x6211;&#x4EEC;&#x5C06;&#x4E00;&#x4E00;&#x4ECB;&#x7ECD;&#x4EE5;&#x4E0A;&#x6B65;&#x9AA4;&#x3002;</p>
<h3 id="&#x4E00;&#x3001;&#x521D;&#x59CB;&#x5316;redis&#x8FDE;&#x63A5;"><a name="&#x4E00;&#x3001;&#x521D;&#x59CB;&#x5316;redis&#x8FDE;&#x63A5;" class="anchor-navigation-ex-anchor" href="#&#x4E00;&#x3001;&#x521D;&#x59CB;&#x5316;redis&#x8FDE;&#x63A5;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x4E00;&#x3001;&#x521D;&#x59CB;&#x5316;redis&#x8FDE;&#x63A5;</h3>
<pre class="language-"><code class="lang-cpp">CacheManager<span class="token operator">*</span> pCacheManager <span class="token operator">=</span> <span class="token class-name">CacheManager</span><span class="token operator">::</span><span class="token function">getInstance</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
<pre class="language-"><code class="lang-cpp">CacheManager<span class="token operator">*</span> <span class="token class-name">CacheManager</span><span class="token operator">::</span><span class="token function">getInstance</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>s_cache_manager<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        s_cache_manager <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token function">CacheManager</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>s_cache_manager<span class="token operator">-&gt;</span><span class="token function">Init</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">delete</span> s_cache_manager<span class="token punctuation">;</span>
            s_cache_manager <span class="token operator">=</span> <span class="token constant">NULL</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">return</span> s_cache_manager<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">int</span> <span class="token class-name">CacheManager</span><span class="token operator">::</span><span class="token function">Init</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    CConfigFileReader <span class="token function">config_file</span><span class="token punctuation">(</span><span class="token string">&quot;dbproxyserver.conf&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">//CacheInstances=unread,group_set,token,sync,group_member</span>
    <span class="token keyword">char</span><span class="token operator">*</span> cache_instances <span class="token operator">=</span> config_file<span class="token punctuation">.</span><span class="token function">GetConfigName</span><span class="token punctuation">(</span><span class="token string">&quot;CacheInstances&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>cache_instances<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;not configure CacheIntance&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">char</span> host<span class="token punctuation">[</span><span class="token number">64</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
    <span class="token keyword">char</span> port<span class="token punctuation">[</span><span class="token number">64</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
    <span class="token keyword">char</span> db<span class="token punctuation">[</span><span class="token number">64</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
    <span class="token keyword">char</span> maxconncnt<span class="token punctuation">[</span><span class="token number">64</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
    CStrExplode <span class="token function">instances_name</span><span class="token punctuation">(</span>cache_instances<span class="token punctuation">,</span> <span class="token string">&apos;,&apos;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">uint32_t</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> instances_name<span class="token punctuation">.</span><span class="token function">GetItemCnt</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">char</span><span class="token operator">*</span> pool_name <span class="token operator">=</span> instances_name<span class="token punctuation">.</span><span class="token function">GetItem</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//printf(&quot;%s&quot;, pool_name);</span>
        <span class="token function">snprintf</span><span class="token punctuation">(</span>host<span class="token punctuation">,</span> <span class="token number">64</span><span class="token punctuation">,</span> <span class="token string">&quot;%s_host&quot;</span><span class="token punctuation">,</span> pool_name<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">snprintf</span><span class="token punctuation">(</span>port<span class="token punctuation">,</span> <span class="token number">64</span><span class="token punctuation">,</span> <span class="token string">&quot;%s_port&quot;</span><span class="token punctuation">,</span> pool_name<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">snprintf</span><span class="token punctuation">(</span>db<span class="token punctuation">,</span> <span class="token number">64</span><span class="token punctuation">,</span> <span class="token string">&quot;%s_db&quot;</span><span class="token punctuation">,</span> pool_name<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">snprintf</span><span class="token punctuation">(</span>maxconncnt<span class="token punctuation">,</span> <span class="token number">64</span><span class="token punctuation">,</span> <span class="token string">&quot;%s_maxconncnt&quot;</span><span class="token punctuation">,</span> pool_name<span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token keyword">char</span><span class="token operator">*</span> cache_host <span class="token operator">=</span> config_file<span class="token punctuation">.</span><span class="token function">GetConfigName</span><span class="token punctuation">(</span>host<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">char</span><span class="token operator">*</span> str_cache_port <span class="token operator">=</span> config_file<span class="token punctuation">.</span><span class="token function">GetConfigName</span><span class="token punctuation">(</span>port<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">char</span><span class="token operator">*</span> str_cache_db <span class="token operator">=</span> config_file<span class="token punctuation">.</span><span class="token function">GetConfigName</span><span class="token punctuation">(</span>db<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">char</span><span class="token operator">*</span> str_max_conn_cnt <span class="token operator">=</span> config_file<span class="token punctuation">.</span><span class="token function">GetConfigName</span><span class="token punctuation">(</span>maxconncnt<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>cache_host <span class="token operator">||</span> <span class="token operator">!</span>str_cache_port <span class="token operator">||</span> <span class="token operator">!</span>str_cache_db <span class="token operator">||</span> <span class="token operator">!</span>str_max_conn_cnt<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;not configure cache instance: %s&quot;</span><span class="token punctuation">,</span> pool_name<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">return</span> <span class="token number">2</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

        CachePool<span class="token operator">*</span> pCachePool <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token function">CachePool</span><span class="token punctuation">(</span>pool_name<span class="token punctuation">,</span> cache_host<span class="token punctuation">,</span> <span class="token function">atoi</span><span class="token punctuation">(</span>str_cache_port<span class="token punctuation">)</span><span class="token punctuation">,</span>
                <span class="token function">atoi</span><span class="token punctuation">(</span>str_cache_db<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">atoi</span><span class="token punctuation">(</span>str_max_conn_cnt<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>pCachePool<span class="token operator">-&gt;</span><span class="token function">Init</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;Init cache pool failed&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">return</span> <span class="token number">3</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

        m_cache_pool_map<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span>pool_name<span class="token punctuation">,</span> pCachePool<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x5728;pCachePool-&gt;Init()&#x4E2D;&#x662F;&#x5B9E;&#x9645;&#x8FDE;&#x63A5;redis&#x7684;&#x52A8;&#x4F5C;&#xFF1A;</p>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">int</span> <span class="token class-name">CachePool</span><span class="token operator">::</span><span class="token function">Init</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> m_cur_conn_cnt<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        CacheConn<span class="token operator">*</span> pConn <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token function">CacheConn</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>pConn<span class="token operator">-&gt;</span><span class="token function">Init</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">delete</span> pConn<span class="token punctuation">;</span>
            <span class="token keyword">return</span> <span class="token number">1</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

        m_free_list<span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span>pConn<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;cache pool: %s, list size: %lu&quot;</span><span class="token punctuation">,</span> m_pool_name<span class="token punctuation">.</span><span class="token function">c_str</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> m_free_list<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>pConn-&gt;Init()&#x8C03;&#x7528;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">int</span> <span class="token class-name">CacheConn</span><span class="token operator">::</span><span class="token function">Init</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>m_pContext<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment">// 4s &#x5C1D;&#x8BD5;&#x91CD;&#x8FDE;&#x4E00;&#x6B21;</span>
    <span class="token keyword">uint64_t</span> cur_time <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">uint64_t</span><span class="token punctuation">)</span><span class="token function">time</span><span class="token punctuation">(</span><span class="token constant">NULL</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>cur_time <span class="token operator">&lt;</span> m_last_connect_time <span class="token operator">+</span> <span class="token number">4</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    m_last_connect_time <span class="token operator">=</span> cur_time<span class="token punctuation">;</span>

    <span class="token comment">// 200ms&#x8D85;&#x65F6;</span>
    <span class="token keyword">struct</span> <span class="token class-name">timeval</span> timeout <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">200000</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
    m_pContext <span class="token operator">=</span> <span class="token function">redisConnectWithTimeout</span><span class="token punctuation">(</span>m_pCachePool<span class="token operator">-&gt;</span><span class="token function">GetServerIP</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> m_pCachePool<span class="token operator">-&gt;</span><span class="token function">GetServerPort</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> timeout<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>m_pContext <span class="token operator">||</span> m_pContext<span class="token operator">-&gt;</span>err<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>m_pContext<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;redisConnect failed: %s&quot;</span><span class="token punctuation">,</span> m_pContext<span class="token operator">-&gt;</span>errstr<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token function">redisFree</span><span class="token punctuation">(</span>m_pContext<span class="token punctuation">)</span><span class="token punctuation">;</span>
            m_pContext <span class="token operator">=</span> <span class="token constant">NULL</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
            <span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;redisConnect failed&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

        <span class="token keyword">return</span> <span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    redisReply<span class="token operator">*</span> reply <span class="token operator">=</span> <span class="token punctuation">(</span>redisReply <span class="token operator">*</span><span class="token punctuation">)</span><span class="token function">redisCommand</span><span class="token punctuation">(</span>m_pContext<span class="token punctuation">,</span> <span class="token string">&quot;SELECT %d&quot;</span><span class="token punctuation">,</span> m_pCachePool<span class="token operator">-&gt;</span><span class="token function">GetDBNum</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>reply <span class="token operator">&amp;&amp;</span> <span class="token punctuation">(</span>reply<span class="token operator">-&gt;</span>type <span class="token operator">==</span> REDIS_REPLY_STATUS<span class="token punctuation">)</span> <span class="token operator">&amp;&amp;</span> <span class="token punctuation">(</span><span class="token function">strncmp</span><span class="token punctuation">(</span>reply<span class="token operator">-&gt;</span>str<span class="token punctuation">,</span> <span class="token string">&quot;OK&quot;</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token function">freeReplyObject</span><span class="token punctuation">(</span>reply<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
        <span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;select cache db failed&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token number">2</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x5C42;&#x7EA7;&#x5173;&#x7CFB;&#x662F;&#x8FD9;&#x6837;&#x7684;&#xFF1A;
CacheManager&#x7684;&#x6210;&#x5458;&#x53D8;&#x91CF;m_cache_pool_map&#x5B58;&#x50A8;&#x4E86;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#x914D;&#x7F6E;&#x7684;redis&#x7F13;&#x5B58;&#x6C60;&#xFF0C;&#x8FD9;&#x662F;&#x4E00;&#x4E2A;map&#x5BF9;&#x8C61;&#xFF0C;key&#x662F;&#x7F13;&#x5B58;&#x6C60;&#x7684;&#x540D;&#x5B57;&#xFF0C;value&#x662F;&#x7F13;&#x5B58;&#x6C60;CachePool&#x5BF9;&#x8C61;&#x7684;&#x6307;&#x9488;&#x3002;</p>
<pre class="language-"><code class="lang-cpp">map<span class="token operator">&lt;</span>string<span class="token punctuation">,</span> CachePool<span class="token operator">*</span><span class="token operator">&gt;</span>    m_cache_pool_map<span class="token punctuation">;</span>
</code></pre>
<p>dbproxyserver.conf&#x76EE;&#x524D;&#x914D;&#x7F6E;&#x4E86;&#x5982;&#x4E0B;&#x51E0;&#x4E2A;redis&#x7F13;&#x5B58;&#x6C60;&#xFF1A;</p>
<pre class="language-"><code class="lang-plain">CacheInstances=unread,group_set,token,sync,group_member
</code></pre>
<p>&#x6BCF;&#x4E00;&#x4E2A;&#x7F13;&#x5B58;&#x6C60;&#x5BF9;&#x8C61;CachePool&#x7684;&#x6210;&#x5458;&#x53D8;&#x91CF;m_free_list&#x4E2D;&#x5B58;&#x50A8;&#x7740;&#x82E5;&#x5E72;&#x4E2A;&#x4E0E;redis&#x7684;&#x8FDE;&#x63A5;&#x5BF9;&#x8C61;&#xFF0C;&#x5177;&#x4F53;&#x662F;&#x591A;&#x5C11;&#x4E2A;&#xFF0C;&#x6839;&#x636E;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#x6765;&#x914D;&#x7F6E;&#x3002;m_free_list&#x5B9A;&#x4E49;&#xFF1A;</p>
<pre class="language-"><code class="lang-cpp">list<span class="token operator">&lt;</span>CacheConn<span class="token operator">*</span><span class="token operator">&gt;</span>    m_free_list<span class="token punctuation">;</span>
</code></pre>
<p>&#x8FD9;&#x4E9B;&#x4E0E;redis&#x8FDE;&#x63A5;&#x5BF9;&#x8C61;&#x540E;&#x9762;&#x4F1A;&#x4ECB;&#x7ECD;&#x5728;&#x4F55;&#x5904;&#x4F7F;&#x7528;&#x3002;</p>
<h3 id="&#x4E8C;&#x3001;&#x521D;&#x59CB;&#x5316;mysql&#x8FDE;&#x63A5;"><a name="&#x4E8C;&#x3001;&#x521D;&#x59CB;&#x5316;mysql&#x8FDE;&#x63A5;" class="anchor-navigation-ex-anchor" href="#&#x4E8C;&#x3001;&#x521D;&#x59CB;&#x5316;mysql&#x8FDE;&#x63A5;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x4E8C;&#x3001;&#x521D;&#x59CB;&#x5316;mysql&#x8FDE;&#x63A5;</h3>
<pre class="language-"><code class="lang-cpp">CDBManager<span class="token operator">*</span> pDBManager <span class="token operator">=</span> <span class="token class-name">CDBManager</span><span class="token operator">::</span><span class="token function">getInstance</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
<pre class="language-"><code class="lang-cpp">CDBManager<span class="token operator">*</span> <span class="token class-name">CDBManager</span><span class="token operator">::</span><span class="token function">getInstance</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>s_db_manager<span class="token punctuation">)</span> 
    <span class="token punctuation">{</span>
        s_db_manager <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token function">CDBManager</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>s_db_manager<span class="token operator">-&gt;</span><span class="token function">Init</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">delete</span> s_db_manager<span class="token punctuation">;</span>
            s_db_manager <span class="token operator">=</span> <span class="token constant">NULL</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">return</span> s_db_manager<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">int</span> <span class="token class-name">CDBManager</span><span class="token operator">::</span><span class="token function">Init</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    CConfigFileReader <span class="token function">config_file</span><span class="token punctuation">(</span><span class="token string">&quot;dbproxyserver.conf&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">//DBInstances=teamtalk_master,teamtalk_slave</span>
    <span class="token keyword">char</span><span class="token operator">*</span> db_instances <span class="token operator">=</span> config_file<span class="token punctuation">.</span><span class="token function">GetConfigName</span><span class="token punctuation">(</span><span class="token string">&quot;DBInstances&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>db_instances<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;not configure DBInstances&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">char</span> host<span class="token punctuation">[</span><span class="token number">64</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
    <span class="token keyword">char</span> port<span class="token punctuation">[</span><span class="token number">64</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
    <span class="token keyword">char</span> dbname<span class="token punctuation">[</span><span class="token number">64</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
    <span class="token keyword">char</span> username<span class="token punctuation">[</span><span class="token number">64</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
    <span class="token keyword">char</span> password<span class="token punctuation">[</span><span class="token number">64</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
    <span class="token keyword">char</span> maxconncnt<span class="token punctuation">[</span><span class="token number">64</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
    CStrExplode <span class="token function">instances_name</span><span class="token punctuation">(</span>db_instances<span class="token punctuation">,</span> <span class="token string">&apos;,&apos;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">uint32_t</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> instances_name<span class="token punctuation">.</span><span class="token function">GetItemCnt</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">char</span><span class="token operator">*</span> pool_name <span class="token operator">=</span> instances_name<span class="token punctuation">.</span><span class="token function">GetItem</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">snprintf</span><span class="token punctuation">(</span>host<span class="token punctuation">,</span> <span class="token number">64</span><span class="token punctuation">,</span> <span class="token string">&quot;%s_host&quot;</span><span class="token punctuation">,</span> pool_name<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">snprintf</span><span class="token punctuation">(</span>port<span class="token punctuation">,</span> <span class="token number">64</span><span class="token punctuation">,</span> <span class="token string">&quot;%s_port&quot;</span><span class="token punctuation">,</span> pool_name<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">snprintf</span><span class="token punctuation">(</span>dbname<span class="token punctuation">,</span> <span class="token number">64</span><span class="token punctuation">,</span> <span class="token string">&quot;%s_dbname&quot;</span><span class="token punctuation">,</span> pool_name<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">snprintf</span><span class="token punctuation">(</span>username<span class="token punctuation">,</span> <span class="token number">64</span><span class="token punctuation">,</span> <span class="token string">&quot;%s_username&quot;</span><span class="token punctuation">,</span> pool_name<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">snprintf</span><span class="token punctuation">(</span>password<span class="token punctuation">,</span> <span class="token number">64</span><span class="token punctuation">,</span> <span class="token string">&quot;%s_password&quot;</span><span class="token punctuation">,</span> pool_name<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">snprintf</span><span class="token punctuation">(</span>maxconncnt<span class="token punctuation">,</span> <span class="token number">64</span><span class="token punctuation">,</span> <span class="token string">&quot;%s_maxconncnt&quot;</span><span class="token punctuation">,</span> pool_name<span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token keyword">char</span><span class="token operator">*</span> db_host <span class="token operator">=</span> config_file<span class="token punctuation">.</span><span class="token function">GetConfigName</span><span class="token punctuation">(</span>host<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">char</span><span class="token operator">*</span> str_db_port <span class="token operator">=</span> config_file<span class="token punctuation">.</span><span class="token function">GetConfigName</span><span class="token punctuation">(</span>port<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">char</span><span class="token operator">*</span> db_dbname <span class="token operator">=</span> config_file<span class="token punctuation">.</span><span class="token function">GetConfigName</span><span class="token punctuation">(</span>dbname<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">char</span><span class="token operator">*</span> db_username <span class="token operator">=</span> config_file<span class="token punctuation">.</span><span class="token function">GetConfigName</span><span class="token punctuation">(</span>username<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">char</span><span class="token operator">*</span> db_password <span class="token operator">=</span> config_file<span class="token punctuation">.</span><span class="token function">GetConfigName</span><span class="token punctuation">(</span>password<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">char</span><span class="token operator">*</span> str_maxconncnt <span class="token operator">=</span> config_file<span class="token punctuation">.</span><span class="token function">GetConfigName</span><span class="token punctuation">(</span>maxconncnt<span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>db_host <span class="token operator">||</span> <span class="token operator">!</span>str_db_port <span class="token operator">||</span> <span class="token operator">!</span>db_dbname <span class="token operator">||</span> <span class="token operator">!</span>db_username <span class="token operator">||</span> <span class="token operator">!</span>db_password <span class="token operator">||</span> <span class="token operator">!</span>str_maxconncnt<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;not configure db instance: %s&quot;</span><span class="token punctuation">,</span> pool_name<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">return</span> <span class="token number">2</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

        <span class="token keyword">int</span> db_port <span class="token operator">=</span> <span class="token function">atoi</span><span class="token punctuation">(</span>str_db_port<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">int</span> db_maxconncnt <span class="token operator">=</span> <span class="token function">atoi</span><span class="token punctuation">(</span>str_maxconncnt<span class="token punctuation">)</span><span class="token punctuation">;</span>
        CDBPool<span class="token operator">*</span> pDBPool <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token function">CDBPool</span><span class="token punctuation">(</span>pool_name<span class="token punctuation">,</span> db_host<span class="token punctuation">,</span> db_port<span class="token punctuation">,</span> db_username<span class="token punctuation">,</span> db_password<span class="token punctuation">,</span> db_dbname<span class="token punctuation">,</span> db_maxconncnt<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>pDBPool<span class="token operator">-&gt;</span><span class="token function">Init</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> 
        <span class="token punctuation">{</span>
            <span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;init db instance failed: %s&quot;</span><span class="token punctuation">,</span> pool_name<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">return</span> <span class="token number">3</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        m_dbpool_map<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span>pool_name<span class="token punctuation">,</span> pDBPool<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x540C;&#x7406;pDBPool-&gt;Init()&#x4E2D;&#x662F;&#x5B9E;&#x9645;&#x8FDE;&#x63A5;mysql&#x4EE3;&#x7801;&#xFF1A;</p>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">int</span> <span class="token class-name">CDBPool</span><span class="token operator">::</span><span class="token function">Init</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> m_db_cur_conn_cnt<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        CDBConn<span class="token operator">*</span> pDBConn <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token function">CDBConn</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">int</span> ret <span class="token operator">=</span> pDBConn<span class="token operator">-&gt;</span><span class="token function">Init</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>ret<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">delete</span> pDBConn<span class="token punctuation">;</span>
            <span class="token keyword">return</span> ret<span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

        m_free_list<span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span>pDBConn<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;db pool: %s, size: %d&quot;</span><span class="token punctuation">,</span> m_pool_name<span class="token punctuation">.</span><span class="token function">c_str</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">)</span>m_free_list<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">int</span> <span class="token class-name">CDBConn</span><span class="token operator">::</span><span class="token function">Init</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    m_mysql <span class="token operator">=</span> <span class="token function">mysql_init</span><span class="token punctuation">(</span><span class="token constant">NULL</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>m_mysql<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;mysql_init failed&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    my_bool reconnect <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
    <span class="token function">mysql_options</span><span class="token punctuation">(</span>m_mysql<span class="token punctuation">,</span> MYSQL_OPT_RECONNECT<span class="token punctuation">,</span> <span class="token operator">&amp;</span>reconnect<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">mysql_options</span><span class="token punctuation">(</span>m_mysql<span class="token punctuation">,</span> MYSQL_SET_CHARSET_NAME<span class="token punctuation">,</span> <span class="token string">&quot;utf8mb4&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span><span class="token function">mysql_real_connect</span><span class="token punctuation">(</span>m_mysql<span class="token punctuation">,</span> m_pDBPool<span class="token operator">-&gt;</span><span class="token function">GetDBServerIP</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> m_pDBPool<span class="token operator">-&gt;</span><span class="token function">GetUsername</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">&quot;&quot;</span><span class="token comment">/*m_pDBPool-&gt;GetPasswrod()*/</span><span class="token punctuation">,</span>
            m_pDBPool<span class="token operator">-&gt;</span><span class="token function">GetDBName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> m_pDBPool<span class="token operator">-&gt;</span><span class="token function">GetDBServerPort</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token constant">NULL</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;mysql_real_connect failed: %s&quot;</span><span class="token punctuation">,</span> <span class="token function">mysql_error</span><span class="token punctuation">(</span>m_mysql<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token number">2</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x4E0E;redis&#x8FDE;&#x63A5;&#x5BF9;&#x8C61;&#x7C7B;&#x4F3C;&#xFF0C;CDBManager&#x7684;&#x6210;&#x5458;&#x5BF9;&#x8C61;m_dbpool_map&#x5B58;&#x50A8;&#x4E86;mysql&#x8FDE;&#x63A5;&#x6C60;&#xFF0C;&#x8FD9;&#x4E5F;&#x662F;&#x4E00;&#x4E2A;stl map&#xFF0C;key&#x662F;&#x6C60;&#x5B50;&#x7684;&#x540D;&#x5B57;&#xFF0C;value&#x662F;&#x8FDE;&#x63A5;&#x6C60;&#x7684;&#x5BF9;&#x8C61;CDBPool&#x6307;&#x9488;&#x3002;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#x4E2D;&#x603B;&#x5171;&#x914D;&#x7F6E;&#x4E86;&#x540D;&#x79F0;&#x4E3A;&#x4E3B;&#x4ECE;&#x4E24;&#x4E2A;mysql&#x8FDE;&#x63A5;&#x6C60;&#x3002;</p>
<pre class="language-"><code class="lang-plain">DBInstances=teamtalk_master,teamtalk_slave
</code></pre>
<p>&#x8FDE;&#x63A5;&#x6C60;&#x5BF9;&#x8C61;CDBPool&#x7528;&#x4E00;&#x4E2A;&#x6210;&#x5458;&#x53D8;&#x91CF;&#x5B58;&#x50A8;&#x81EA;&#x5DF1;&#x7684;&#x82E5;&#x5E72;&#x4E2A;mysql&#x8FDE;&#x63A5;&#xFF1A;</p>
<pre class="language-"><code class="lang-cpp">list<span class="token operator">&lt;</span>CDBConn<span class="token operator">*</span><span class="token operator">&gt;</span>    m_free_list<span class="token punctuation">;</span>        <span class="token comment">//&#x5B9E;&#x9645;&#x4FDD;&#x5B58;mysql&#x8FDE;&#x63A5;&#x7684;&#x5BB9;&#x5668;</span>
</code></pre>
<p>&#x5177;&#x4F53;&#x6BCF;&#x4E2A;&#x8FDE;&#x63A5;&#x6C60;&#x6709;&#x591A;&#x5C11;&#x4E2A;mysql&#x8FDE;&#x63A5;&#xFF0C;&#x6839;&#x636E;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#x5F97;&#x5230;&#xFF0C;&#x8FD9;&#x91CC;&#x4E3B;&#x4ECE;&#x4E24;&#x4E2A;&#x5E93;&#x90FD;&#x662F;16&#x4E2A;&#x3002;</p>
<p>&#x8FD9;&#x4E9B;mysql&#x8FDE;&#x63A5;&#x7684;&#x7528;&#x9014;&#x540E;&#x9762;&#x4ECB;&#x7ECD;&#x3002;</p>
<h3 id="&#x4E09;&#x3001;&#x542F;&#x52A8;&#x4EFB;&#x52A1;&#x961F;&#x5217;&#xFF0C;&#x7528;&#x4E8E;&#x5904;&#x7406;&#x4EFB;&#x52A1;"><a name="&#x4E09;&#x3001;&#x542F;&#x52A8;&#x4EFB;&#x52A1;&#x961F;&#x5217;&#xFF0C;&#x7528;&#x4E8E;&#x5904;&#x7406;&#x4EFB;&#x52A1;" class="anchor-navigation-ex-anchor" href="#&#x4E09;&#x3001;&#x542F;&#x52A8;&#x4EFB;&#x52A1;&#x961F;&#x5217;&#xFF0C;&#x7528;&#x4E8E;&#x5904;&#x7406;&#x4EFB;&#x52A1;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x4E09;&#x3001;&#x542F;&#x52A8;&#x4EFB;&#x52A1;&#x961F;&#x5217;&#xFF0C;&#x7528;&#x4E8E;&#x5904;&#x7406;&#x4EFB;&#x52A1;</h3>
<p>&#x521D;&#x59CB;&#x5316;&#x4E00;&#xFF1A;&#x521B;&#x5EFA;&#x7EBF;&#x7A0B;&#x5904;&#x7406;&#x4EFB;&#x52A1;&#x961F;&#x5217;&#x4E2D;&#x7684;&#x4EFB;&#x52A1;</p>
<pre class="language-"><code class="lang-cpp"><span class="token function">init_proxy_conn</span><span class="token punctuation">(</span>thread_num<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">int</span> <span class="token function">init_proxy_conn</span><span class="token punctuation">(</span><span class="token keyword">uint32_t</span> thread_num<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    s_handler_map <span class="token operator">=</span> <span class="token class-name">CHandlerMap</span><span class="token operator">::</span><span class="token function">getInstance</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    g_thread_pool<span class="token punctuation">.</span><span class="token function">Init</span><span class="token punctuation">(</span>thread_num<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token function">netlib_add_loop</span><span class="token punctuation">(</span>proxy_loop_callback<span class="token punctuation">,</span> <span class="token constant">NULL</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token function">signal</span><span class="token punctuation">(</span>SIGTERM<span class="token punctuation">,</span> sig_handler<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">return</span> <span class="token function">netlib_register_timer</span><span class="token punctuation">(</span>proxy_timer_callback<span class="token punctuation">,</span> <span class="token constant">NULL</span><span class="token punctuation">,</span> <span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x7EBF;&#x7A0B;&#x6570;&#x91CF;&#x6839;&#x636E;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#x5F97;&#x5230;&#x3002;g_thread_pool.Init(thread_num)&#x4E2D;&#x5B9E;&#x9645;&#x521B;&#x5EFA;&#x5904;&#x7406;&#x4EFB;&#x52A1;&#x7684;&#x7EBF;&#x7A0B;&#x3002;</p>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">int</span> <span class="token class-name">CThreadPool</span><span class="token operator">::</span><span class="token function">Init</span><span class="token punctuation">(</span><span class="token keyword">uint32_t</span> worker_size<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    m_worker_size <span class="token operator">=</span> worker_size<span class="token punctuation">;</span>
    m_worker_list <span class="token operator">=</span> <span class="token keyword">new</span> CWorkerThread <span class="token punctuation">[</span>m_worker_size<span class="token punctuation">]</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>m_worker_list<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">uint32_t</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> m_worker_size<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        m_worker_list<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">SetThreadIdx</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span>
        m_worker_list<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">Start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">void</span> <span class="token class-name">CWorkerThread</span><span class="token operator">::</span><span class="token function">Start</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span><span class="token function">pthread_create</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>m_thread_id<span class="token punctuation">,</span> <span class="token constant">NULL</span><span class="token punctuation">,</span> StartRoutine<span class="token punctuation">,</span> <span class="token keyword">this</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x7EBF;&#x7A0B;&#x51FD;&#x6570;&#x8C03;&#x7528;&#x5E8F;&#x5217;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">void</span><span class="token operator">*</span> <span class="token class-name">CWorkerThread</span><span class="token operator">::</span><span class="token function">StartRoutine</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token operator">*</span> arg<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    CWorkerThread<span class="token operator">*</span> pThread <span class="token operator">=</span> <span class="token punctuation">(</span>CWorkerThread<span class="token operator">*</span><span class="token punctuation">)</span>arg<span class="token punctuation">;</span>

    pThread<span class="token operator">-&gt;</span><span class="token function">Execute</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">return</span> <span class="token constant">NULL</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">void</span> <span class="token class-name">CWorkerThread</span><span class="token operator">::</span><span class="token function">Execute</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        m_thread_notify<span class="token punctuation">.</span><span class="token function">Lock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">// put wait in while cause there can be spurious wake up (due to signal/ENITR)</span>
        <span class="token keyword">while</span> <span class="token punctuation">(</span>m_task_list<span class="token punctuation">.</span><span class="token function">empty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            m_thread_notify<span class="token punctuation">.</span><span class="token function">Wait</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

        CTask<span class="token operator">*</span> pTask <span class="token operator">=</span> m_task_list<span class="token punctuation">.</span><span class="token function">front</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        m_task_list<span class="token punctuation">.</span><span class="token function">pop_front</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        m_thread_notify<span class="token punctuation">.</span><span class="token function">Unlock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        pTask<span class="token operator">-&gt;</span><span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token keyword">delete</span> pTask<span class="token punctuation">;</span>

        m_task_cnt<span class="token operator">++</span><span class="token punctuation">;</span>
        <span class="token comment">//log(&quot;%d have the execute %d task\n&quot;, m_thread_idx, m_task_cnt);</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x53EF;&#x4EE5;&#x770B;&#x5230;&#x5DE5;&#x4F5C;&#x7EBF;&#x7A0B;&#x4E00;&#x76F4;&#x5728;&#x7B49;&#x5F85;&#x4E00;&#x4E2A;&#x6761;&#x4EF6;&#x53D8;&#x91CF;&#xFF0C;&#x5F53;&#x5411;&#x4EFB;&#x52A1;&#x961F;&#x5217;&#x4E2D;&#x6DFB;&#x52A0;&#x4EFB;&#x52A1;&#x65F6;&#xFF0C;&#x6761;&#x4EF6;&#x53D8;&#x91CF;&#x88AB;&#x5524;&#x9192;&#xFF1A;</p>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">void</span> <span class="token class-name">CWorkerThread</span><span class="token operator">::</span><span class="token function">PushTask</span><span class="token punctuation">(</span>CTask<span class="token operator">*</span> pTask<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    m_thread_notify<span class="token punctuation">.</span><span class="token function">Lock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    m_task_list<span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span>pTask<span class="token punctuation">)</span><span class="token punctuation">;</span>
    m_thread_notify<span class="token punctuation">.</span><span class="token function">Signal</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    m_thread_notify<span class="token punctuation">.</span><span class="token function">Unlock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x4EFB;&#x52A1;&#x961F;&#x5217;&#x7684;&#x7528;&#x9014;&#xFF0C;&#x4E0B;&#x6587;&#x4F1A;&#x4ECB;&#x7ECD;&#x3002;</p>
<p>&#x521D;&#x59CB;&#x5316;&#x4E8C;&#xFF1A;&#x5C06;&#x5404;&#x4E2A;&#x4EFB;&#x52A1;id&#x4E0E;&#x5BF9;&#x5E94;&#x7684;&#x5904;&#x7406;&#x51FD;&#x6570;&#x7ED1;&#x5B9A;&#x8D77;&#x6765;&#xFF1A;</p>
<pre class="language-"><code class="lang-cpp">s_handler_map <span class="token operator">=</span> <span class="token class-name">CHandlerMap</span><span class="token operator">::</span><span class="token function">getInstance</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
<pre class="language-"><code class="lang-cpp">CHandlerMap<span class="token operator">*</span> <span class="token class-name">CHandlerMap</span><span class="token operator">::</span><span class="token function">getInstance</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>s_handler_instance<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        s_handler_instance <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token function">CHandlerMap</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        s_handler_instance<span class="token operator">-&gt;</span><span class="token function">Init</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">return</span> s_handler_instance<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">void</span> <span class="token class-name">CHandlerMap</span><span class="token operator">::</span><span class="token function">Init</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token comment">//DB_PROXY&#x662F;&#x547D;&#x540D;&#x7A7A;&#x95F4;&#xFF0C;&#x4E0D;&#x662F;&#x7C7B;&#x540D;</span>
    <span class="token comment">// Login validate</span>
    m_handler_map<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span><span class="token keyword">uint32_t</span><span class="token punctuation">(</span>CID_OTHER_VALIDATE_REQ<span class="token punctuation">)</span><span class="token punctuation">,</span> DB_PROXY<span class="token operator">::</span>doLogin<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    m_handler_map<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span><span class="token keyword">uint32_t</span><span class="token punctuation">(</span>CID_LOGIN_REQ_PUSH_SHIELD<span class="token punctuation">)</span><span class="token punctuation">,</span> DB_PROXY<span class="token operator">::</span>doPushShield<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    m_handler_map<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span><span class="token keyword">uint32_t</span><span class="token punctuation">(</span>CID_LOGIN_REQ_QUERY_PUSH_SHIELD<span class="token punctuation">)</span><span class="token punctuation">,</span> DB_PROXY<span class="token operator">::</span>doQueryPushShield<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">// recent session</span>
    m_handler_map<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span><span class="token keyword">uint32_t</span><span class="token punctuation">(</span>CID_BUDDY_LIST_RECENT_CONTACT_SESSION_REQUEST<span class="token punctuation">)</span><span class="token punctuation">,</span> DB_PROXY<span class="token operator">::</span>getRecentSession<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    m_handler_map<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span><span class="token keyword">uint32_t</span><span class="token punctuation">(</span>CID_BUDDY_LIST_REMOVE_SESSION_REQ<span class="token punctuation">)</span><span class="token punctuation">,</span> DB_PROXY<span class="token operator">::</span>deleteRecentSession<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">// users</span>
    m_handler_map<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span><span class="token keyword">uint32_t</span><span class="token punctuation">(</span>CID_BUDDY_LIST_USER_INFO_REQUEST<span class="token punctuation">)</span><span class="token punctuation">,</span> DB_PROXY<span class="token operator">::</span>getUserInfo<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    m_handler_map<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span><span class="token keyword">uint32_t</span><span class="token punctuation">(</span>CID_BUDDY_LIST_ALL_USER_REQUEST<span class="token punctuation">)</span><span class="token punctuation">,</span> DB_PROXY<span class="token operator">::</span>getChangedUser<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    m_handler_map<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span><span class="token keyword">uint32_t</span><span class="token punctuation">(</span>CID_BUDDY_LIST_DEPARTMENT_REQUEST<span class="token punctuation">)</span><span class="token punctuation">,</span> DB_PROXY<span class="token operator">::</span>getChgedDepart<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    m_handler_map<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span><span class="token keyword">uint32_t</span><span class="token punctuation">(</span>CID_BUDDY_LIST_CHANGE_SIGN_INFO_REQUEST<span class="token punctuation">)</span><span class="token punctuation">,</span> DB_PROXY<span class="token operator">::</span>changeUserSignInfo<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>


    <span class="token comment">// message content</span>
    m_handler_map<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span><span class="token keyword">uint32_t</span><span class="token punctuation">(</span>CID_MSG_DATA<span class="token punctuation">)</span><span class="token punctuation">,</span> DB_PROXY<span class="token operator">::</span>sendMessage<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    m_handler_map<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span><span class="token keyword">uint32_t</span><span class="token punctuation">(</span>CID_MSG_LIST_REQUEST<span class="token punctuation">)</span><span class="token punctuation">,</span> DB_PROXY<span class="token operator">::</span>getMessage<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    m_handler_map<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span><span class="token keyword">uint32_t</span><span class="token punctuation">(</span>CID_MSG_UNREAD_CNT_REQUEST<span class="token punctuation">)</span><span class="token punctuation">,</span> DB_PROXY<span class="token operator">::</span>getUnreadMsgCounter<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    m_handler_map<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span><span class="token keyword">uint32_t</span><span class="token punctuation">(</span>CID_MSG_READ_ACK<span class="token punctuation">)</span><span class="token punctuation">,</span> DB_PROXY<span class="token operator">::</span>clearUnreadMsgCounter<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    m_handler_map<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span><span class="token keyword">uint32_t</span><span class="token punctuation">(</span>CID_MSG_GET_BY_MSG_ID_REQ<span class="token punctuation">)</span><span class="token punctuation">,</span> DB_PROXY<span class="token operator">::</span>getMessageById<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    m_handler_map<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span><span class="token keyword">uint32_t</span><span class="token punctuation">(</span>CID_MSG_GET_LATEST_MSG_ID_REQ<span class="token punctuation">)</span><span class="token punctuation">,</span> DB_PROXY<span class="token operator">::</span>getLatestMsgId<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">// device token</span>
    m_handler_map<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span><span class="token keyword">uint32_t</span><span class="token punctuation">(</span>CID_LOGIN_REQ_DEVICETOKEN<span class="token punctuation">)</span><span class="token punctuation">,</span> DB_PROXY<span class="token operator">::</span>setDevicesToken<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    m_handler_map<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span><span class="token keyword">uint32_t</span><span class="token punctuation">(</span>CID_OTHER_GET_DEVICE_TOKEN_REQ<span class="token punctuation">)</span><span class="token punctuation">,</span> DB_PROXY<span class="token operator">::</span>getDevicesToken<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">//push &#x63A8;&#x9001;&#x8BBE;&#x7F6E;</span>
    m_handler_map<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span><span class="token keyword">uint32_t</span><span class="token punctuation">(</span>CID_GROUP_SHIELD_GROUP_REQUEST<span class="token punctuation">)</span><span class="token punctuation">,</span> DB_PROXY<span class="token operator">::</span>setGroupPush<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    m_handler_map<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span><span class="token keyword">uint32_t</span><span class="token punctuation">(</span>CID_OTHER_GET_SHIELD_REQ<span class="token punctuation">)</span><span class="token punctuation">,</span> DB_PROXY<span class="token operator">::</span>getGroupPush<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>


    <span class="token comment">// group</span>
    m_handler_map<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span><span class="token keyword">uint32_t</span><span class="token punctuation">(</span>CID_GROUP_NORMAL_LIST_REQUEST<span class="token punctuation">)</span><span class="token punctuation">,</span> DB_PROXY<span class="token operator">::</span>getNormalGroupList<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    m_handler_map<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span><span class="token keyword">uint32_t</span><span class="token punctuation">(</span>CID_GROUP_INFO_REQUEST<span class="token punctuation">)</span><span class="token punctuation">,</span> DB_PROXY<span class="token operator">::</span>getGroupInfo<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    m_handler_map<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span><span class="token keyword">uint32_t</span><span class="token punctuation">(</span>CID_GROUP_CREATE_REQUEST<span class="token punctuation">)</span><span class="token punctuation">,</span> DB_PROXY<span class="token operator">::</span>createGroup<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    m_handler_map<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span><span class="token keyword">uint32_t</span><span class="token punctuation">(</span>CID_GROUP_CHANGE_MEMBER_REQUEST<span class="token punctuation">)</span><span class="token punctuation">,</span> DB_PROXY<span class="token operator">::</span>modifyMember<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>


    <span class="token comment">// file</span>
    m_handler_map<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span><span class="token keyword">uint32_t</span><span class="token punctuation">(</span>CID_FILE_HAS_OFFLINE_REQ<span class="token punctuation">)</span><span class="token punctuation">,</span> DB_PROXY<span class="token operator">::</span>hasOfflineFile<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    m_handler_map<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span><span class="token keyword">uint32_t</span><span class="token punctuation">(</span>CID_FILE_ADD_OFFLINE_REQ<span class="token punctuation">)</span><span class="token punctuation">,</span> DB_PROXY<span class="token operator">::</span>addOfflineFile<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    m_handler_map<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span><span class="token keyword">uint32_t</span><span class="token punctuation">(</span>CID_FILE_DEL_OFFLINE_REQ<span class="token punctuation">)</span><span class="token punctuation">,</span> DB_PROXY<span class="token operator">::</span>delOfflineFile<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>    m_handler_map<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span><span class="token keyword">uint32_t</span><span class="token punctuation">(</span>CID_OTHER_VALIDATE_REQ<span class="token punctuation">)</span><span class="token punctuation">,</span> DB_PROXY<span class="token operator">::</span>doLogin<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    m_handler_map<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span><span class="token keyword">uint32_t</span><span class="token punctuation">(</span>CID_LOGIN_REQ_PUSH_SHIELD<span class="token punctuation">)</span><span class="token punctuation">,</span> DB_PROXY<span class="token operator">::</span>doPushShield<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    m_handler_map<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span><span class="token keyword">uint32_t</span><span class="token punctuation">(</span>CID_LOGIN_REQ_QUERY_PUSH_SHIELD<span class="token punctuation">)</span><span class="token punctuation">,</span> DB_PROXY<span class="token operator">::</span>doQueryPushShield<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">// recent session</span>
    m_handler_map<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span><span class="token keyword">uint32_t</span><span class="token punctuation">(</span>CID_BUDDY_LIST_RECENT_CONTACT_SESSION_REQUEST<span class="token punctuation">)</span><span class="token punctuation">,</span> DB_PROXY<span class="token operator">::</span>getRecentSession<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    m_handler_map<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span><span class="token keyword">uint32_t</span><span class="token punctuation">(</span>CID_BUDDY_LIST_REMOVE_SESSION_REQ<span class="token punctuation">)</span><span class="token punctuation">,</span> DB_PROXY<span class="token operator">::</span>deleteRecentSession<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">// users</span>
    m_handler_map<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span><span class="token keyword">uint32_t</span><span class="token punctuation">(</span>CID_BUDDY_LIST_USER_INFO_REQUEST<span class="token punctuation">)</span><span class="token punctuation">,</span> DB_PROXY<span class="token operator">::</span>getUserInfo<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    m_handler_map<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span><span class="token keyword">uint32_t</span><span class="token punctuation">(</span>CID_BUDDY_LIST_ALL_USER_REQUEST<span class="token punctuation">)</span><span class="token punctuation">,</span> DB_PROXY<span class="token operator">::</span>getChangedUser<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    m_handler_map<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span><span class="token keyword">uint32_t</span><span class="token punctuation">(</span>CID_BUDDY_LIST_DEPARTMENT_REQUEST<span class="token punctuation">)</span><span class="token punctuation">,</span> DB_PROXY<span class="token operator">::</span>getChgedDepart<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    m_handler_map<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span><span class="token keyword">uint32_t</span><span class="token punctuation">(</span>CID_BUDDY_LIST_CHANGE_SIGN_INFO_REQUEST<span class="token punctuation">)</span><span class="token punctuation">,</span> DB_PROXY<span class="token operator">::</span>changeUserSignInfo<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>


    <span class="token comment">// message content</span>
    m_handler_map<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span><span class="token keyword">uint32_t</span><span class="token punctuation">(</span>CID_MSG_DATA<span class="token punctuation">)</span><span class="token punctuation">,</span> DB_PROXY<span class="token operator">::</span>sendMessage<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    m_handler_map<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span><span class="token keyword">uint32_t</span><span class="token punctuation">(</span>CID_MSG_LIST_REQUEST<span class="token punctuation">)</span><span class="token punctuation">,</span> DB_PROXY<span class="token operator">::</span>getMessage<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    m_handler_map<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span><span class="token keyword">uint32_t</span><span class="token punctuation">(</span>CID_MSG_UNREAD_CNT_REQUEST<span class="token punctuation">)</span><span class="token punctuation">,</span> DB_PROXY<span class="token operator">::</span>getUnreadMsgCounter<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    m_handler_map<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span><span class="token keyword">uint32_t</span><span class="token punctuation">(</span>CID_MSG_READ_ACK<span class="token punctuation">)</span><span class="token punctuation">,</span> DB_PROXY<span class="token operator">::</span>clearUnreadMsgCounter<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    m_handler_map<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span><span class="token keyword">uint32_t</span><span class="token punctuation">(</span>CID_MSG_GET_BY_MSG_ID_REQ<span class="token punctuation">)</span><span class="token punctuation">,</span> DB_PROXY<span class="token operator">::</span>getMessageById<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    m_handler_map<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span><span class="token keyword">uint32_t</span><span class="token punctuation">(</span>CID_MSG_GET_LATEST_MSG_ID_REQ<span class="token punctuation">)</span><span class="token punctuation">,</span> DB_PROXY<span class="token operator">::</span>getLatestMsgId<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">// device token</span>
    m_handler_map<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span><span class="token keyword">uint32_t</span><span class="token punctuation">(</span>CID_LOGIN_REQ_DEVICETOKEN<span class="token punctuation">)</span><span class="token punctuation">,</span> DB_PROXY<span class="token operator">::</span>setDevicesToken<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    m_handler_map<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span><span class="token keyword">uint32_t</span><span class="token punctuation">(</span>CID_OTHER_GET_DEVICE_TOKEN_REQ<span class="token punctuation">)</span><span class="token punctuation">,</span> DB_PROXY<span class="token operator">::</span>getDevicesToken<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">//push &#x63A8;&#x9001;&#x8BBE;&#x7F6E;</span>
    m_handler_map<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span><span class="token keyword">uint32_t</span><span class="token punctuation">(</span>CID_GROUP_SHIELD_GROUP_REQUEST<span class="token punctuation">)</span><span class="token punctuation">,</span> DB_PROXY<span class="token operator">::</span>setGroupPush<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    m_handler_map<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span><span class="token keyword">uint32_t</span><span class="token punctuation">(</span>CID_OTHER_GET_SHIELD_REQ<span class="token punctuation">)</span><span class="token punctuation">,</span> DB_PROXY<span class="token operator">::</span>getGroupPush<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>


    <span class="token comment">// group</span>
    m_handler_map<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span><span class="token keyword">uint32_t</span><span class="token punctuation">(</span>CID_GROUP_NORMAL_LIST_REQUEST<span class="token punctuation">)</span><span class="token punctuation">,</span> DB_PROXY<span class="token operator">::</span>getNormalGroupList<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    m_handler_map<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span><span class="token keyword">uint32_t</span><span class="token punctuation">(</span>CID_GROUP_INFO_REQUEST<span class="token punctuation">)</span><span class="token punctuation">,</span> DB_PROXY<span class="token operator">::</span>getGroupInfo<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    m_handler_map<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span><span class="token keyword">uint32_t</span><span class="token punctuation">(</span>CID_GROUP_CREATE_REQUEST<span class="token punctuation">)</span><span class="token punctuation">,</span> DB_PROXY<span class="token operator">::</span>createGroup<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    m_handler_map<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span><span class="token keyword">uint32_t</span><span class="token punctuation">(</span>CID_GROUP_CHANGE_MEMBER_REQUEST<span class="token punctuation">)</span><span class="token punctuation">,</span> DB_PROXY<span class="token operator">::</span>modifyMember<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>


    <span class="token comment">// file</span>
    m_handler_map<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span><span class="token keyword">uint32_t</span><span class="token punctuation">(</span>CID_FILE_HAS_OFFLINE_REQ<span class="token punctuation">)</span><span class="token punctuation">,</span> DB_PROXY<span class="token operator">::</span>hasOfflineFile<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    m_handler_map<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span><span class="token keyword">uint32_t</span><span class="token punctuation">(</span>CID_FILE_ADD_OFFLINE_REQ<span class="token punctuation">)</span><span class="token punctuation">,</span> DB_PROXY<span class="token operator">::</span>addOfflineFile<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    m_handler_map<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span><span class="token keyword">uint32_t</span><span class="token punctuation">(</span>CID_FILE_DEL_OFFLINE_REQ<span class="token punctuation">)</span><span class="token punctuation">,</span> DB_PROXY<span class="token operator">::</span>delOfflineFile<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<h3 id="&#x56DB;&#x3001;&#x542F;&#x52A8;&#x4ECE;mysql&#x540C;&#x6B65;&#x6570;&#x636E;&#x5230;redis&#x5DE5;&#x4F5C;"><a name="&#x56DB;&#x3001;&#x542F;&#x52A8;&#x4ECE;mysql&#x540C;&#x6B65;&#x6570;&#x636E;&#x5230;redis&#x5DE5;&#x4F5C;" class="anchor-navigation-ex-anchor" href="#&#x56DB;&#x3001;&#x542F;&#x52A8;&#x4ECE;mysql&#x540C;&#x6B65;&#x6570;&#x636E;&#x5230;redis&#x5DE5;&#x4F5C;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x56DB;&#x3001;&#x542F;&#x52A8;&#x4ECE;mysql&#x540C;&#x6B65;&#x6570;&#x636E;&#x5230;redis&#x5DE5;&#x4F5C;</h3>
<pre class="language-"><code class="lang-cpp"><span class="token class-name">CSyncCenter</span><span class="token operator">::</span><span class="token function">getInstance</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-&gt;</span><span class="token function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">CSyncCenter</span><span class="token operator">::</span><span class="token function">getInstance</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-&gt;</span><span class="token function">startSync</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
<p>CSyncCenter::getInstance()-&gt;init()&#x662F;&#x83B7;&#x5F97;&#x4E0A;&#x6B21;&#x540C;&#x6B65;&#x7684;&#x6570;&#x636E;&#x4F4D;&#x7F6E;&#xFF0C;&#x63A5;&#x4E0B;&#x6765;&#x540C;&#x6B65;&#x4ECE;&#x8FD9;&#x4E2A;&#x4F4D;&#x7F6E;&#x5F00;&#x59CB;&#x3002;</p>
<pre class="language-"><code class="lang-cpp"><span class="token comment">/*
 * &#x521D;&#x59CB;&#x5316;&#x51FD;&#x6570;&#xFF0C;&#x4ECE;cache&#x91CC;&#x9762;&#x52A0;&#x8F7D;&#x4E0A;&#x6B21;&#x540C;&#x6B65;&#x7684;&#x65F6;&#x95F4;&#x4FE1;&#x606F;&#x7B49;
 */</span>
<span class="token keyword">void</span> <span class="token class-name">CSyncCenter</span><span class="token operator">::</span><span class="token function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token comment">// Load total update time</span>
    CacheManager<span class="token operator">*</span> pCacheManager <span class="token operator">=</span> <span class="token class-name">CacheManager</span><span class="token operator">::</span><span class="token function">getInstance</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// increase message count</span>
    CacheConn<span class="token operator">*</span> pCacheConn <span class="token operator">=</span> pCacheManager<span class="token operator">-&gt;</span><span class="token function">GetCacheConn</span><span class="token punctuation">(</span><span class="token string">&quot;unread&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>pCacheConn<span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
        string strTotalUpdate <span class="token operator">=</span> pCacheConn<span class="token operator">-&gt;</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string">&quot;total_user_updated&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        string strLastUpdateGroup <span class="token operator">=</span> pCacheConn<span class="token operator">-&gt;</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string">&quot;last_update_group&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        pCacheManager<span class="token operator">-&gt;</span><span class="token function">RelCacheConn</span><span class="token punctuation">(</span>pCacheConn<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>strTotalUpdate <span class="token operator">!=</span> <span class="token string">&quot;&quot;</span><span class="token punctuation">)</span>
        <span class="token punctuation">{</span>
            m_nLastUpdate <span class="token operator">=</span> <span class="token function">string2int</span><span class="token punctuation">(</span>strTotalUpdate<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">else</span>
        <span class="token punctuation">{</span>
            <span class="token function">updateTotalUpdate</span><span class="token punctuation">(</span><span class="token function">time</span><span class="token punctuation">(</span><span class="token constant">NULL</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span>strLastUpdateGroup<span class="token punctuation">.</span><span class="token function">empty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token punctuation">{</span>
            m_nLastUpdateGroup <span class="token operator">=</span> <span class="token function">string2int</span><span class="token punctuation">(</span>strLastUpdateGroup<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">else</span>
        <span class="token punctuation">{</span>
            <span class="token function">updateLastUpdateGroup</span><span class="token punctuation">(</span><span class="token function">time</span><span class="token punctuation">(</span><span class="token constant">NULL</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">else</span>
    <span class="token punctuation">{</span>
        <span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;no cache connection to get total_user_updated&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>CSyncCenter::getInstance()-&gt;startSync();&#x65B0;&#x5F00;&#x542F;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x8FDB;&#x884C;&#x540C;&#x6B65;&#x5DE5;&#x4F5C;&#xFF1A;</p>
<pre class="language-"><code class="lang-cpp"><span class="token comment">/**
 *  &#x5F00;&#x542F;&#x5185;&#x7F51;&#x6570;&#x636E;&#x540C;&#x6B65;&#x4EE5;&#x53CA;&#x7FA4;&#x7EC4;&#x804A;&#x5929;&#x8BB0;&#x5F55;&#x540C;&#x6B65;
 */</span>
<span class="token keyword">void</span> <span class="token class-name">CSyncCenter</span><span class="token operator">::</span><span class="token function">startSync</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">ifdef</span> <span class="token expression">_WIN32</span></span>
    <span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span><span class="token function">CreateThread</span><span class="token punctuation">(</span><span class="token constant">NULL</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> doSyncGroupChat<span class="token punctuation">,</span> <span class="token constant">NULL</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>m_nGroupChatThreadId<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">else</span></span>
    <span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span><span class="token function">pthread_create</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>m_nGroupChatThreadId<span class="token punctuation">,</span> <span class="token constant">NULL</span><span class="token punctuation">,</span> doSyncGroupChat<span class="token punctuation">,</span> <span class="token constant">NULL</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">endif</span></span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x7EBF;&#x7A0B;&#x51FD;&#x6570;doSyncGroupChat()&#x5982;&#x4E0B;&#xFF1A;</p>
<pre class="language-"><code class="lang-cpp"><span class="token comment">/**
 *  &#x540C;&#x6B65;&#x7FA4;&#x7EC4;&#x804A;&#x5929;&#x4FE1;&#x606F;
 *
 *  @param arg NULL
 *
 *  @return NULL
 */</span>
<span class="token keyword">void</span><span class="token operator">*</span> <span class="token class-name">CSyncCenter</span><span class="token operator">::</span><span class="token function">doSyncGroupChat</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token operator">*</span> arg<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    m_bSyncGroupChatRuning <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
    CDBManager<span class="token operator">*</span> pDBManager <span class="token operator">=</span> <span class="token class-name">CDBManager</span><span class="token operator">::</span><span class="token function">getInstance</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    map<span class="token operator">&lt;</span><span class="token keyword">uint32_t</span><span class="token punctuation">,</span> <span class="token keyword">uint32_t</span><span class="token operator">&gt;</span> mapChangedGroup<span class="token punctuation">;</span>
    <span class="token keyword">do</span><span class="token punctuation">{</span>
        mapChangedGroup<span class="token punctuation">.</span><span class="token function">clear</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        CDBConn<span class="token operator">*</span> pDBConn <span class="token operator">=</span> pDBManager<span class="token operator">-&gt;</span><span class="token function">GetDBConn</span><span class="token punctuation">(</span><span class="token string">&quot;teamtalk_slave&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span>pDBConn<span class="token punctuation">)</span>
        <span class="token punctuation">{</span>
            string strSql <span class="token operator">=</span> <span class="token string">&quot;select id, lastChated from IMGroup where status=0 and lastChated &gt;=&quot;</span> <span class="token operator">+</span> <span class="token function">int2string</span><span class="token punctuation">(</span>m_pInstance<span class="token operator">-&gt;</span><span class="token function">getLastUpdateGroup</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            CResultSet<span class="token operator">*</span> pResult <span class="token operator">=</span> pDBConn<span class="token operator">-&gt;</span><span class="token function">ExecuteQuery</span><span class="token punctuation">(</span>strSql<span class="token punctuation">.</span><span class="token function">c_str</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">if</span><span class="token punctuation">(</span>pResult<span class="token punctuation">)</span>
            <span class="token punctuation">{</span>
                <span class="token keyword">while</span> <span class="token punctuation">(</span>pResult<span class="token operator">-&gt;</span><span class="token function">Next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                    <span class="token keyword">uint32_t</span> nGroupId <span class="token operator">=</span> pResult<span class="token operator">-&gt;</span><span class="token function">GetInt</span><span class="token punctuation">(</span><span class="token string">&quot;id&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token keyword">uint32_t</span> nLastChat <span class="token operator">=</span> pResult<span class="token operator">-&gt;</span><span class="token function">GetInt</span><span class="token punctuation">(</span><span class="token string">&quot;lastChated&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token keyword">if</span><span class="token punctuation">(</span>nLastChat <span class="token operator">!=</span> <span class="token number">0</span><span class="token punctuation">)</span>
                    <span class="token punctuation">{</span>
                        mapChangedGroup<span class="token punctuation">[</span>nGroupId<span class="token punctuation">]</span> <span class="token operator">=</span> nLastChat<span class="token punctuation">;</span>
                    <span class="token punctuation">}</span>
                <span class="token punctuation">}</span>
                <span class="token keyword">delete</span> pResult<span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
            pDBManager<span class="token operator">-&gt;</span><span class="token function">RelDBConn</span><span class="token punctuation">(</span>pDBConn<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">else</span>
        <span class="token punctuation">{</span>
            <span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;no db connection for teamtalk_slave&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        m_pInstance<span class="token operator">-&gt;</span><span class="token function">updateLastUpdateGroup</span><span class="token punctuation">(</span><span class="token function">time</span><span class="token punctuation">(</span><span class="token constant">NULL</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">auto</span> it<span class="token operator">=</span>mapChangedGroup<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> it<span class="token operator">!=</span>mapChangedGroup<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token operator">++</span>it<span class="token punctuation">)</span>
        <span class="token punctuation">{</span>
            <span class="token keyword">uint32_t</span> nGroupId <span class="token operator">=</span>it<span class="token operator">-&gt;</span>first<span class="token punctuation">;</span>
            list<span class="token operator">&lt;</span><span class="token keyword">uint32_t</span><span class="token operator">&gt;</span> lsUsers<span class="token punctuation">;</span>
            <span class="token keyword">uint32_t</span> nUpdate <span class="token operator">=</span> it<span class="token operator">-&gt;</span>second<span class="token punctuation">;</span>
            <span class="token class-name">CGroupModel</span><span class="token operator">::</span><span class="token function">getInstance</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-&gt;</span><span class="token function">getGroupUser</span><span class="token punctuation">(</span>nGroupId<span class="token punctuation">,</span> lsUsers<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">auto</span> it1<span class="token operator">=</span>lsUsers<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> it1<span class="token operator">!=</span>lsUsers<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token operator">++</span>it1<span class="token punctuation">)</span>
            <span class="token punctuation">{</span>
                <span class="token keyword">uint32_t</span> nUserId <span class="token operator">=</span> <span class="token operator">*</span>it1<span class="token punctuation">;</span>
                <span class="token keyword">uint32_t</span> nSessionId <span class="token operator">=</span> INVALID_VALUE<span class="token punctuation">;</span>
                nSessionId <span class="token operator">=</span> <span class="token class-name">CSessionModel</span><span class="token operator">::</span><span class="token function">getInstance</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-&gt;</span><span class="token function">getSessionId</span><span class="token punctuation">(</span>nUserId<span class="token punctuation">,</span> nGroupId<span class="token punctuation">,</span> IM<span class="token operator">::</span>BaseDefine<span class="token operator">::</span>SESSION_TYPE_GROUP<span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token keyword">if</span><span class="token punctuation">(</span>nSessionId <span class="token operator">!=</span> INVALID_VALUE<span class="token punctuation">)</span>
                <span class="token punctuation">{</span>
                    <span class="token class-name">CSessionModel</span><span class="token operator">::</span><span class="token function">getInstance</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-&gt;</span><span class="token function">updateSession</span><span class="token punctuation">(</span>nSessionId<span class="token punctuation">,</span> nUpdate<span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token punctuation">}</span>
                <span class="token keyword">else</span>
                <span class="token punctuation">{</span>
                    <span class="token class-name">CSessionModel</span><span class="token operator">::</span><span class="token function">getInstance</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-&gt;</span><span class="token function">addSession</span><span class="token punctuation">(</span>nUserId<span class="token punctuation">,</span> nGroupId<span class="token punctuation">,</span> IM<span class="token operator">::</span>BaseDefine<span class="token operator">::</span>SESSION_TYPE_GROUP<span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
<span class="token comment">//    } while (!m_pInstance-&gt;m_pCondSync-&gt;waitTime(5*1000));</span>
    <span class="token punctuation">}</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>m_pInstance<span class="token operator">-&gt;</span>m_bSyncGroupChatWaitting <span class="token operator">&amp;&amp;</span> <span class="token operator">!</span><span class="token punctuation">(</span>m_pInstance<span class="token operator">-&gt;</span>m_pCondGroupChat<span class="token operator">-&gt;</span><span class="token function">waitTime</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token operator">*</span><span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//    } while(m_pInstance-&gt;m_bSyncGroupChatWaitting);</span>
    m_bSyncGroupChatRuning <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token constant">NULL</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x53EF;&#x4EE5;&#x770B;&#x5230;&#x6D41;&#x7A0B;&#x5C31;&#x662F;&#x5148;&#x7528;sql&#x4ECE;mysql&#x53D6;&#x51FA;&#x6570;&#x636E;&#xFF0C;&#x518D;&#x7528;&#x201C;sql&#x201D;&#x5199;&#x5230;redis&#x4E2D;&#x53BB;&#x3002;&#x64CD;&#x4F5C;mysql&#x548C;redis&#x65F6;&#xFF0C;&#x5E76;&#x6CA1;&#x6709;&#x65B0;&#x5EFA;&#x65B0;&#x8FDE;&#x63A5;&#xFF0C;&#x800C;&#x662F;&#x4F7F;&#x7528;&#x4E0A;&#x6587;&#x4ECB;&#x7ECD;&#x7684;&#x8FDE;&#x63A5;&#x6C60;&#x548C;&#x7F13;&#x5B58;&#x6C60;&#x4E2D;&#x5DF2;&#x6709;&#x7684;&#x8FDE;&#x63A5;&#x3002;&#x6211;&#x4EEC;&#x4E0A;&#x6587;&#x8BF4;&#x4E86;&#xFF0C;&#x6BCF;&#x4E2A;&#x6C60;&#x4E2D;&#x90FD;&#x6709;&#x82E5;&#x5E72;&#x4E2A;&#x8FDE;&#x63A5;&#xFF0C;&#x90A3;&#x4F7F;&#x7528;&#x54EA;&#x4E2A;&#x8FDE;&#x63A5;&#x5462;&#xFF1F;&#x7531;&#x4E8E;&#x4FDD;&#x5B58;mysql&#x7684;&#x8FDE;&#x63A5;&#x662F;&#x4E00;&#x4E2A;list&#x5BF9;&#x8C61;&#xFF0C;&#x6240;&#x4EE5;&#x9ED8;&#x8BA4;&#x4ECE;list&#x7684;&#x5934;&#x90E8;&#x53D6;&#x4E00;&#x4E2A;&#x53EF;&#x7528;&#x7684;&#x3002;&#x5982;&#x679C;&#x5F53;&#x524D;&#x6CA1;&#x6709;&#x7A7A;&#x95F2;&#x8FDE;&#x63A5;&#x53EF;&#x7528;&#xFF0C;&#x5219;&#x65B0;&#x5EFA;&#x4E00;&#x4E2A;&#xFF1A;</p>
<pre class="language-"><code class="lang-cpp">CDBConn<span class="token operator">*</span> <span class="token class-name">CDBPool</span><span class="token operator">::</span><span class="token function">GetDBConn</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    m_free_notify<span class="token punctuation">.</span><span class="token function">Lock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">while</span> <span class="token punctuation">(</span>m_free_list<span class="token punctuation">.</span><span class="token function">empty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>m_db_cur_conn_cnt <span class="token operator">&gt;=</span> m_db_max_conn_cnt<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            m_free_notify<span class="token punctuation">.</span><span class="token function">Wait</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
            CDBConn<span class="token operator">*</span> pDBConn <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token function">CDBConn</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">int</span> ret <span class="token operator">=</span> pDBConn<span class="token operator">-&gt;</span><span class="token function">Init</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>ret<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;Init DBConnecton failed&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token keyword">delete</span> pDBConn<span class="token punctuation">;</span>
                m_free_notify<span class="token punctuation">.</span><span class="token function">Unlock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token keyword">return</span> <span class="token constant">NULL</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
                m_free_list<span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span>pDBConn<span class="token punctuation">)</span><span class="token punctuation">;</span>
                m_db_cur_conn_cnt<span class="token operator">++</span><span class="token punctuation">;</span>
                <span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;new db connection: %s, conn_cnt: %d&quot;</span><span class="token punctuation">,</span> m_pool_name<span class="token punctuation">.</span><span class="token function">c_str</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> m_db_cur_conn_cnt<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

    CDBConn<span class="token operator">*</span> pConn <span class="token operator">=</span> m_free_list<span class="token punctuation">.</span><span class="token function">front</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    m_free_list<span class="token punctuation">.</span><span class="token function">pop_front</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    m_free_notify<span class="token punctuation">.</span><span class="token function">Unlock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">return</span> pConn<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x5206;&#x914D;redis&#x548C;mysql&#x7684;&#x4E00;&#x6A21;&#x4E00;&#x6837;&#xFF0C;&#x8FD9;&#x91CC;&#x4EE3;&#x7801;&#x5C31;&#x4E0D;&#x8D34;&#x4E86;&#x3002;</p>
<h3 id="&#x4E94;&#x3001;&#x5728;&#x7AEF;&#x53E3;10600&#x4E0A;&#x542F;&#x52A8;&#x4FA6;&#x542C;&#xFF0C;&#x76D1;&#x542C;&#x65B0;&#x8FDE;&#x63A5;"><a name="&#x4E94;&#x3001;&#x5728;&#x7AEF;&#x53E3;10600&#x4E0A;&#x542F;&#x52A8;&#x4FA6;&#x542C;&#xFF0C;&#x76D1;&#x542C;&#x65B0;&#x8FDE;&#x63A5;" class="anchor-navigation-ex-anchor" href="#&#x4E94;&#x3001;&#x5728;&#x7AEF;&#x53E3;10600&#x4E0A;&#x542F;&#x52A8;&#x4FA6;&#x542C;&#xFF0C;&#x76D1;&#x542C;&#x65B0;&#x8FDE;&#x63A5;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x4E94;&#x3001;&#x5728;&#x7AEF;&#x53E3;10600&#x4E0A;&#x542F;&#x52A8;&#x4FA6;&#x542C;&#xFF0C;&#x76D1;&#x542C;&#x65B0;&#x8FDE;&#x63A5;</h3>
<pre class="language-"><code class="lang-cpp">CStrExplode <span class="token function">listen_ip_list</span><span class="token punctuation">(</span>listen_ip<span class="token punctuation">,</span> <span class="token string">&apos;;&apos;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">uint32_t</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> listen_ip_list<span class="token punctuation">.</span><span class="token function">GetItemCnt</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    ret <span class="token operator">=</span> <span class="token function">netlib_listen</span><span class="token punctuation">(</span>listen_ip_list<span class="token punctuation">.</span><span class="token function">GetItem</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">,</span> listen_port<span class="token punctuation">,</span> proxy_serv_callback<span class="token punctuation">,</span> <span class="token constant">NULL</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>ret <span class="token operator">==</span> NETLIB_ERROR<span class="token punctuation">)</span>
        <span class="token keyword">return</span> ret<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>netlib_listen()&#x521B;&#x5EFA;CBaseSocket&#x5BF9;&#x8C61;&#xFF0C;&#x5E76;&#x5C06;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x6307;&#x9488;proxy_serv_callback&#x4FDD;&#x5B58;&#x5728;CBaseSocket&#x5BF9;&#x8C61;&#x4E2D;&#x3002;</p>
<pre class="language-"><code class="lang-csharp"><span class="token return-type class-name"><span class="token keyword">int</span></span> <span class="token function">netlib_listen</span><span class="token punctuation">(</span>
        <span class="token keyword">const</span> <span class="token keyword">char</span><span class="token operator">*</span>    server_ip<span class="token punctuation">,</span> 
        <span class="token class-name">uint16_t</span>    port<span class="token punctuation">,</span>
        <span class="token class-name">callback_t</span>    callback<span class="token punctuation">,</span>
        <span class="token keyword">void</span><span class="token operator">*</span>        callback_data<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    CBaseSocket<span class="token operator">*</span> pSocket <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token constructor-invocation class-name">CBaseSocket</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>pSocket<span class="token punctuation">)</span>
        <span class="token keyword">return</span> NETLIB_ERROR<span class="token punctuation">;</span>

    <span class="token class-name"><span class="token keyword">int</span></span> ret <span class="token operator">=</span>  pSocket<span class="token operator">-&gt;</span><span class="token function">Listen</span><span class="token punctuation">(</span>server_ip<span class="token punctuation">,</span> port<span class="token punctuation">,</span> callback<span class="token punctuation">,</span> callback_data<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>ret <span class="token operator">==</span> NETLIB_ERROR<span class="token punctuation">)</span>
        <span class="token class-name">delete</span> pSocket<span class="token punctuation">;</span>
    <span class="token keyword">return</span> ret<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>pSocket-&gt;Listen()&#x662F;&#x5B9E;&#x9645;&#x8C03;&#x7528;bind()&#x548C;listen()&#x51FD;&#x6570;&#x521B;&#x5EFA;&#x4FA6;&#x542C;&#x7684;&#x5730;&#x65B9;&#x3002;</p>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">int</span> <span class="token class-name">CBaseSocket</span><span class="token operator">::</span><span class="token function">Listen</span><span class="token punctuation">(</span><span class="token keyword">const</span> <span class="token keyword">char</span><span class="token operator">*</span> server_ip<span class="token punctuation">,</span> <span class="token keyword">uint16_t</span> port<span class="token punctuation">,</span> callback_t callback<span class="token punctuation">,</span> <span class="token keyword">void</span><span class="token operator">*</span> callback_data<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    m_local_ip <span class="token operator">=</span> server_ip<span class="token punctuation">;</span>
    m_local_port <span class="token operator">=</span> port<span class="token punctuation">;</span>
    m_callback <span class="token operator">=</span> callback<span class="token punctuation">;</span>
    m_callback_data <span class="token operator">=</span> callback_data<span class="token punctuation">;</span>

    m_socket <span class="token operator">=</span> <span class="token function">socket</span><span class="token punctuation">(</span>AF_INET<span class="token punctuation">,</span> SOCK_STREAM<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>m_socket <span class="token operator">==</span> INVALID_SOCKET<span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
        <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;socket failed, err_code=%d\n&quot;</span><span class="token punctuation">,</span> <span class="token function">_GetErrorCode</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> NETLIB_ERROR<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token function">_SetReuseAddr</span><span class="token punctuation">(</span>m_socket<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">_SetNonblock</span><span class="token punctuation">(</span>m_socket<span class="token punctuation">)</span><span class="token punctuation">;</span>

    sockaddr_in serv_addr<span class="token punctuation">;</span>
    <span class="token function">_SetAddr</span><span class="token punctuation">(</span>server_ip<span class="token punctuation">,</span> port<span class="token punctuation">,</span> <span class="token operator">&amp;</span>serv_addr<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">int</span> ret <span class="token operator">=</span> <span class="token operator">::</span><span class="token function">bind</span><span class="token punctuation">(</span>m_socket<span class="token punctuation">,</span> <span class="token punctuation">(</span>sockaddr<span class="token operator">*</span><span class="token punctuation">)</span><span class="token operator">&amp;</span>serv_addr<span class="token punctuation">,</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span>serv_addr<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>ret <span class="token operator">==</span> SOCKET_ERROR<span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
        <span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;bind failed, err_code=%d&quot;</span><span class="token punctuation">,</span> <span class="token function">_GetErrorCode</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">closesocket</span><span class="token punctuation">(</span>m_socket<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> NETLIB_ERROR<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    ret <span class="token operator">=</span> <span class="token function">listen</span><span class="token punctuation">(</span>m_socket<span class="token punctuation">,</span> <span class="token number">64</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>ret <span class="token operator">==</span> SOCKET_ERROR<span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
        <span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;listen failed, err_code=%d&quot;</span><span class="token punctuation">,</span> <span class="token function">_GetErrorCode</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">closesocket</span><span class="token punctuation">(</span>m_socket<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> NETLIB_ERROR<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    m_state <span class="token operator">=</span> SOCKET_STATE_LISTENING<span class="token punctuation">;</span>

    <span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;CBaseSocket::Listen on %s:%d&quot;</span><span class="token punctuation">,</span> server_ip<span class="token punctuation">,</span> port<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token function">AddBaseSocket</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">CEventDispatch</span><span class="token operator">::</span><span class="token function">Instance</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-&gt;</span><span class="token function">AddEvent</span><span class="token punctuation">(</span>m_socket<span class="token punctuation">,</span> SOCKET_READ <span class="token operator">|</span> SOCKET_EXCEP<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> NETLIB_OK<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x8FD9;&#x4E2A;&#x51FD;&#x6570;&#x6709;&#x5927;&#x91CF;&#x7684;&#x7EC6;&#x8282;&#x9700;&#x8981;&#x6CE8;&#x610F;&#xFF1A;</p>
<ol>
<li><p>socket&#x88AB;&#x8BBE;&#x7F6E;&#x6210;&#x975E;&#x963B;&#x585E;&#x6A21;&#x5F0F;&#xFF1B;</p>
</li>
<li><p>&#x5C06;&#x7ED1;&#x5B9A;&#x7684;&#x5730;&#x5740;&#x8BBE;&#x7F6E;&#x6210;reuse&#xFF08;&#x5177;&#x4F53;&#x539F;&#x56E0;&#xFF0C;&#x6211;&#x5728;&#x300A;&#x670D;&#x52A1;&#x5668;&#x7F16;&#x7A0B;&#x5FC3;&#x5F97;&#x300B;&#x7CFB;&#x5217;&#x5DF2;&#x7ECF;&#x4ECB;&#x7ECD;&#x8FC7;&#xFF09;</p>
</li>
<li><p>&#x5C06;socket&#x7684;&#x72B6;&#x6001;&#x8BBE;&#x7F6E;&#x6210;SOCKET_STATE_LISTENING&#xFF0C;&#x8FD9;&#x4E2A;&#x72B6;&#x6001;&#x5C06;&#x4FA6;&#x542C;&#x7684;socket&#x4E0E;&#x666E;&#x901A;&#x5BA2;&#x6237;&#x7AEF;&#x8FDE;&#x63A5;&#x7684;socket&#x533A;&#x522B;&#x5F00;&#x6765;&#x3002;</p>
</li>
<li><p>AddBaseSocket(this);&#x5C06;socket&#x53E5;&#x67C4;&#x548C;&#x5BF9;&#x5E94;&#x7684;CBaseSocket&#x653E;&#x5230;&#x4E00;&#x4E2A;&#x5168;&#x5C40;&#x5BF9;&#x8C61;&#x4E2D;&#x7BA1;&#x7406;&#x8D77;&#x6765;&#x3002;</p>
</li>
</ol>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">typedef</span> hash_map<span class="token operator">&lt;</span>net_handle_t<span class="token punctuation">,</span> CBaseSocket<span class="token operator">*</span><span class="token operator">&gt;</span> SocketMap<span class="token punctuation">;</span>
SocketMap    g_socket_map<span class="token punctuation">;</span>

<span class="token keyword">void</span> <span class="token function">AddBaseSocket</span><span class="token punctuation">(</span>CBaseSocket<span class="token operator">*</span> pSocket<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    g_socket_map<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span><span class="token punctuation">(</span>net_handle_t<span class="token punctuation">)</span>pSocket<span class="token operator">-&gt;</span><span class="token function">GetSocket</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> pSocket<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x4E4B;&#x6240;&#x4EE5;&#x4E0D;&#x7528;map&#x800C;&#x7528;hash_map&#x662F;&#x56E0;&#x4E3A;STL&#x7684;map&#x5E95;&#x5C42;&#x662F;&#x7528;&#x7EA2;&#x9ED1;&#x6811;&#x5B9E;&#x73B0;&#x7684;&#xFF0C;&#x67E5;&#x627E;&#x65F6;&#x95F4;&#x590D;&#x6742;&#x5EA6;&#x662F;log(n)&#xFF0C;&#x800C;hash_map&#x5E95;&#x5C42;&#x662F;&#x7528;hash&#x8868;&#x5B58;&#x50A8;&#x7684;&#xFF0C;&#x67E5;&#x8BE2;&#x65F6;&#x95F4;&#x590D;&#x6742;&#x5EA6;&#x662F;O(1)&#x3002;&#x540E;&#x9762;&#x4F1A;&#x4ECB;&#x7ECD;&#x5C06;&#x5728;&#x8FD9;&#x4E2A;hash_map&#x4E2D;&#x67E5;&#x627E;&#x6240;&#x6709;&#x7684;socket&#x3002;</p>
<ol>
<li>&#x76EE;&#x524D;&#x53EA;&#x5173;&#x6CE8;socket&#x7684;&#x8BFB;&#x548C;&#x5F02;&#x5E38;&#x4E8B;&#x4EF6;&#xFF0C;&#x4FA6;&#x542C;socket&#x53EF;&#x8BFB;&#x610F;&#x5473;&#x7740;&#x6709;&#x65B0;&#x8FDE;&#x63A5;&#x5230;&#x6765;&#xFF0C;&#x5F02;&#x5E38;&#x5C31;&#x610F;&#x5473;&#x7740;&#x4FA6;&#x542C;&#x51FA;&#x9519;&#x3002;&#x5BF9;&#x4E8E;&#x670D;&#x52A1;&#x5668;&#x7A0B;&#x5E8F;&#x4E00;&#x822C;&#x8981;&#x5173;&#x95ED;&#x6216;&#x91CD;&#x542F;&#x670D;&#x52A1;&#x3002;</li>
</ol>
<h3 id="&#x516D;&#x3001;&#x4E3B;&#x7EBF;&#x7A0B;&#x8FDB;&#x5165;&#x5FAA;&#x73AF;&#xFF0C;&#x76D1;&#x542C;&#x65B0;&#x8FDE;&#x63A5;&#x7684;&#x5230;&#x6765;&#x4EE5;&#x53CA;&#x51FA;&#x6765;&#x65B0;&#x8FDE;&#x63A5;&#x4E0A;&#x7684;&#x6570;&#x636E;&#x6536;&#x53D1;"><a name="&#x516D;&#x3001;&#x4E3B;&#x7EBF;&#x7A0B;&#x8FDB;&#x5165;&#x5FAA;&#x73AF;&#xFF0C;&#x76D1;&#x542C;&#x65B0;&#x8FDE;&#x63A5;&#x7684;&#x5230;&#x6765;&#x4EE5;&#x53CA;&#x51FA;&#x6765;&#x65B0;&#x8FDE;&#x63A5;&#x4E0A;&#x7684;&#x6570;&#x636E;&#x6536;&#x53D1;" class="anchor-navigation-ex-anchor" href="#&#x516D;&#x3001;&#x4E3B;&#x7EBF;&#x7A0B;&#x8FDB;&#x5165;&#x5FAA;&#x73AF;&#xFF0C;&#x76D1;&#x542C;&#x65B0;&#x8FDE;&#x63A5;&#x7684;&#x5230;&#x6765;&#x4EE5;&#x53CA;&#x51FA;&#x6765;&#x65B0;&#x8FDE;&#x63A5;&#x4E0A;&#x7684;&#x6570;&#x636E;&#x6536;&#x53D1;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x516D;&#x3001;&#x4E3B;&#x7EBF;&#x7A0B;&#x8FDB;&#x5165;&#x5FAA;&#x73AF;&#xFF0C;&#x76D1;&#x542C;&#x65B0;&#x8FDE;&#x63A5;&#x7684;&#x5230;&#x6765;&#x4EE5;&#x53CA;&#x51FA;&#x6765;&#x65B0;&#x8FDE;&#x63A5;&#x4E0A;&#x7684;&#x6570;&#x636E;&#x6536;&#x53D1;</h3>
<pre class="language-"><code class="lang-cpp"><span class="token function">netlib_eventloop</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span>
</code></pre>
<p>10&#x662F;&#x8D85;&#x65F6;&#x65F6;&#x95F4;&#xFF0C;&#x7528;&#x4E8E;select()&#x51FD;&#x6570;&#x7684;&#x8C03;&#x7528;&#xFF1A;</p>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">void</span> <span class="token function">netlib_eventloop</span><span class="token punctuation">(</span><span class="token keyword">uint32_t</span> wait_timeout<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token class-name">CEventDispatch</span><span class="token operator">::</span><span class="token function">Instance</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-&gt;</span><span class="token function">StartDispatch</span><span class="token punctuation">(</span>wait_timeout<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">void</span> <span class="token class-name">CEventDispatch</span><span class="token operator">::</span><span class="token function">StartDispatch</span><span class="token punctuation">(</span><span class="token keyword">uint32_t</span> wait_timeout<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    fd_set read_set<span class="token punctuation">,</span> write_set<span class="token punctuation">,</span> excep_set<span class="token punctuation">;</span>
    timeval timeout<span class="token punctuation">;</span>
    timeout<span class="token punctuation">.</span>tv_sec <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
    timeout<span class="token punctuation">.</span>tv_usec <span class="token operator">=</span> wait_timeout <span class="token operator">*</span> <span class="token number">1000</span><span class="token punctuation">;</span>    <span class="token comment">// 10 millisecond</span>

    <span class="token keyword">if</span><span class="token punctuation">(</span>running<span class="token punctuation">)</span>
        <span class="token keyword">return</span><span class="token punctuation">;</span>
    running <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span>

    <span class="token keyword">while</span> <span class="token punctuation">(</span>running<span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
        <span class="token function">_CheckTimer</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">_CheckLoop</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>m_read_set<span class="token punctuation">.</span>fd_count <span class="token operator">&amp;&amp;</span> <span class="token operator">!</span>m_write_set<span class="token punctuation">.</span>fd_count <span class="token operator">&amp;&amp;</span> <span class="token operator">!</span>m_excep_set<span class="token punctuation">.</span>fd_count<span class="token punctuation">)</span>
        <span class="token punctuation">{</span>
            <span class="token function">Sleep</span><span class="token punctuation">(</span>MIN_TIMER_DURATION<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">continue</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

        m_lock<span class="token punctuation">.</span><span class="token function">lock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">memcpy</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>read_set<span class="token punctuation">,</span> <span class="token operator">&amp;</span>m_read_set<span class="token punctuation">,</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span>fd_set<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">memcpy</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>write_set<span class="token punctuation">,</span> <span class="token operator">&amp;</span>m_write_set<span class="token punctuation">,</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span>fd_set<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">memcpy</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>excep_set<span class="token punctuation">,</span> <span class="token operator">&amp;</span>m_excep_set<span class="token punctuation">,</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span>fd_set<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        m_lock<span class="token punctuation">.</span><span class="token function">unlock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token keyword">int</span> nfds <span class="token operator">=</span> <span class="token function">select</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>read_set<span class="token punctuation">,</span> <span class="token operator">&amp;</span>write_set<span class="token punctuation">,</span> <span class="token operator">&amp;</span>excep_set<span class="token punctuation">,</span> <span class="token operator">&amp;</span>timeout<span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token keyword">if</span> <span class="token punctuation">(</span>nfds <span class="token operator">==</span> SOCKET_ERROR<span class="token punctuation">)</span>
        <span class="token punctuation">{</span>
            <span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;select failed, error code: %d&quot;</span><span class="token punctuation">,</span> <span class="token function">GetLastError</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token function">Sleep</span><span class="token punctuation">(</span>MIN_TIMER_DURATION<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">continue</span><span class="token punctuation">;</span>            <span class="token comment">// select again</span>
        <span class="token punctuation">}</span>

        <span class="token keyword">if</span> <span class="token punctuation">(</span>nfds <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span>
        <span class="token punctuation">{</span>
            <span class="token keyword">continue</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

        <span class="token keyword">for</span> <span class="token punctuation">(</span>u_int i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> read_set<span class="token punctuation">.</span>fd_count<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span>
        <span class="token punctuation">{</span>
            <span class="token comment">//log(&quot;select return read count=%d\n&quot;, read_set.fd_count);</span>
            SOCKET fd <span class="token operator">=</span> read_set<span class="token punctuation">.</span>fd_array<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
            CBaseSocket<span class="token operator">*</span> pSocket <span class="token operator">=</span> <span class="token function">FindBaseSocket</span><span class="token punctuation">(</span><span class="token punctuation">(</span>net_handle_t<span class="token punctuation">)</span>fd<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>pSocket<span class="token punctuation">)</span>
            <span class="token punctuation">{</span>
                pSocket<span class="token operator">-&gt;</span><span class="token function">OnRead</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                pSocket<span class="token operator">-&gt;</span><span class="token function">ReleaseRef</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>

        <span class="token keyword">for</span> <span class="token punctuation">(</span>u_int i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> write_set<span class="token punctuation">.</span>fd_count<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span>
        <span class="token punctuation">{</span>
            <span class="token comment">//log(&quot;select return write count=%d\n&quot;, write_set.fd_count);</span>
            SOCKET fd <span class="token operator">=</span> write_set<span class="token punctuation">.</span>fd_array<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
            CBaseSocket<span class="token operator">*</span> pSocket <span class="token operator">=</span> <span class="token function">FindBaseSocket</span><span class="token punctuation">(</span><span class="token punctuation">(</span>net_handle_t<span class="token punctuation">)</span>fd<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>pSocket<span class="token punctuation">)</span>
            <span class="token punctuation">{</span>
                pSocket<span class="token operator">-&gt;</span><span class="token function">OnWrite</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                pSocket<span class="token operator">-&gt;</span><span class="token function">ReleaseRef</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>

        <span class="token keyword">for</span> <span class="token punctuation">(</span>u_int i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> excep_set<span class="token punctuation">.</span>fd_count<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span>
        <span class="token punctuation">{</span>
            <span class="token comment">//log(&quot;select return exception count=%d\n&quot;, excep_set.fd_count);</span>
            SOCKET fd <span class="token operator">=</span> excep_set<span class="token punctuation">.</span>fd_array<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
            CBaseSocket<span class="token operator">*</span> pSocket <span class="token operator">=</span> <span class="token function">FindBaseSocket</span><span class="token punctuation">(</span><span class="token punctuation">(</span>net_handle_t<span class="token punctuation">)</span>fd<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>pSocket<span class="token punctuation">)</span>
            <span class="token punctuation">{</span>
                pSocket<span class="token operator">-&gt;</span><span class="token function">OnClose</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                pSocket<span class="token operator">-&gt;</span><span class="token function">ReleaseRef</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>

    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x8FD9;&#x4E2A;&#x51FD;&#x6570;&#x662F;&#x6574;&#x4E2A;&#x670D;&#x52A1;&#x7A0B;&#x5E8F;&#x7684;&#x52A8;&#x529B;&#x548C;&#x6D88;&#x606F;&#x6CF5;&#x3002;&#x6211;&#x628A;&#x5B83;&#x7B80;&#x5316;&#x6210;&#x5982;&#x4E0B;&#x4F2A;&#x7801;&#x6765;&#x91CD;&#x70B9;&#x4ECB;&#x7ECD;&#x4E00;&#x4E0B;&#xFF1A;</p>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">while</span><span class="token punctuation">(</span>&#x9000;&#x51FA;&#x6761;&#x4EF6;<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token comment">//1. &#x904D;&#x5386;&#x5B9A;&#x65F6;&#x5668;&#x961F;&#x5217;&#xFF0C;&#x68C0;&#x6D4B;&#x662F;&#x5426;&#x6709;&#x5B9A;&#x65F6;&#x5668;&#x4E8B;&#x4EF6;&#x5230;&#x671F;&#xFF0C;&#x6709;&#x5219;&#x6267;&#x884C;&#x5B9A;&#x65F6;&#x5668;&#x7684;&#x56DE;&#x8C03;&#x51FD;&#x6570;</span>

    <span class="token comment">//2. &#x904D;&#x5386;&#x5176;&#x4ED6;&#x4EFB;&#x52A1;&#x961F;&#x5217;&#xFF0C;&#x68C0;&#x6D4B;&#x662F;&#x5426;&#x6709;&#x5176;&#x4ED6;&#x4EFB;&#x52A1;&#x9700;&#x8981;&#x6267;&#x884C;&#xFF0C;&#x6709;&#xFF0C;&#x6267;&#x884C;&#x4E4B;</span>

    <span class="token comment">//3. &#x68C0;&#x6D4B;socket&#x96C6;&#x5408;&#xFF0C;&#x5206;&#x79BB;&#x53EF;&#x8BFB;&#x3001;&#x53EF;&#x5199;&#x548C;&#x5F02;&#x5E38;&#x4E8B;&#x4EF6;</span>

    <span class="token comment">//4. &#x5904;&#x7406;socket&#x53EF;&#x8BFB;&#x4E8B;&#x4EF6;</span>

    <span class="token comment">//5. &#x5904;&#x7406;socket&#x53EF;&#x5199;&#x4E8B;&#x4EF6;</span>

    <span class="token comment">//6. &#x5904;&#x7406;socket&#x5F02;&#x5E38;&#x4E8B;&#x4EF6;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x6211;&#x4EEC;&#x5148;&#x4E0D;&#x8BF4;1&#x3001;2&#x4E24;&#x70B9;&#xFF0C;&#x5F53;&#x7A0B;&#x5E8F;&#x521D;&#x59CB;&#x5316;&#x540E;&#xFF0C;socket&#x96C6;&#x5408;&#x4E2D;&#xFF0C;&#x4E5F;&#x53EA;&#x6709;&#x4E00;&#x4E2A;socket&#xFF0C;&#x5C31;&#x662F;&#x4E0A;&#x6587;&#x4E2D;&#x8BF4;&#x7684;&#x4FA6;&#x542C;socket&#x3002;&#x5F53;&#x6709;&#x65B0;&#x8FDE;&#x63A5;&#x6765;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x8BE5;socket&#x88AB;&#x68C0;&#x6D4B;&#x5230;&#x53EF;&#x8BFB;&#x3002;&#x6267;&#x884C;</p>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">for</span> <span class="token punctuation">(</span>u_int i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> read_set<span class="token punctuation">.</span>fd_count<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token comment">//log(&quot;select return read count=%d\n&quot;, read_set.fd_count);</span>
    SOCKET fd <span class="token operator">=</span> read_set<span class="token punctuation">.</span>fd_array<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
    CBaseSocket<span class="token operator">*</span> pSocket <span class="token operator">=</span> <span class="token function">FindBaseSocket</span><span class="token punctuation">(</span><span class="token punctuation">(</span>net_handle_t<span class="token punctuation">)</span>fd<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>pSocket<span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
        pSocket<span class="token operator">-&gt;</span><span class="token function">OnRead</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        pSocket<span class="token operator">-&gt;</span><span class="token function">ReleaseRef</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>    <span class="token comment">//log(&quot;select return read count=%d\n&quot;, read_set.fd_count);</span>
    SOCKET fd <span class="token operator">=</span> read_set<span class="token punctuation">.</span>fd_array<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
    CBaseSocket<span class="token operator">*</span> pSocket <span class="token operator">=</span> <span class="token function">FindBaseSocket</span><span class="token punctuation">(</span><span class="token punctuation">(</span>net_handle_t<span class="token punctuation">)</span>fd<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>pSocket<span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
        pSocket<span class="token operator">-&gt;</span><span class="token function">OnRead</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        pSocket<span class="token operator">-&gt;</span><span class="token function">ReleaseRef</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>FindBaseSocket()&#x5C31;&#x662F;&#x5728;&#x4E0A;&#x6587;&#x63D0;&#x5230;&#x7684;socket&#x96C6;&#x5408;map&#x4E2D;&#x901A;&#x8FC7;&#x53E5;&#x67C4;&#x67E5;&#x627E;socket&#xFF1A;</p>
<pre class="language-"><code class="lang-cpp">CBaseSocket<span class="token operator">*</span> <span class="token function">FindBaseSocket</span><span class="token punctuation">(</span>net_handle_t fd<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    CBaseSocket<span class="token operator">*</span> pSocket <span class="token operator">=</span> <span class="token constant">NULL</span><span class="token punctuation">;</span>
    SocketMap<span class="token operator">::</span>iterator iter <span class="token operator">=</span> g_socket_map<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span>fd<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>iter <span class="token operator">!=</span> g_socket_map<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
        pSocket <span class="token operator">=</span> iter<span class="token operator">-&gt;</span>second<span class="token punctuation">;</span>
        pSocket<span class="token operator">-&gt;</span><span class="token function">AddRef</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">return</span> pSocket<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x63A5;&#x7740;&#x6267;&#x884C;pSocket-&gt;OnRead():</p>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">void</span> <span class="token class-name">CBaseSocket</span><span class="token operator">::</span><span class="token function">OnRead</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>m_state <span class="token operator">==</span> SOCKET_STATE_LISTENING<span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
        <span class="token function">_AcceptNewSocket</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">else</span>
    <span class="token punctuation">{</span>
        u_long avail <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span> <span class="token punctuation">(</span><span class="token function">ioctlsocket</span><span class="token punctuation">(</span>m_socket<span class="token punctuation">,</span> FIONREAD<span class="token punctuation">,</span> <span class="token operator">&amp;</span>avail<span class="token punctuation">)</span> <span class="token operator">==</span> SOCKET_ERROR<span class="token punctuation">)</span> <span class="token operator">||</span> <span class="token punctuation">(</span>avail <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">)</span>
        <span class="token punctuation">{</span>
            <span class="token function">m_callback</span><span class="token punctuation">(</span>m_callback_data<span class="token punctuation">,</span> NETLIB_MSG_CLOSE<span class="token punctuation">,</span> <span class="token punctuation">(</span>net_handle_t<span class="token punctuation">)</span>m_socket<span class="token punctuation">,</span> <span class="token constant">NULL</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">else</span>
        <span class="token punctuation">{</span>
            <span class="token function">m_callback</span><span class="token punctuation">(</span>m_callback_data<span class="token punctuation">,</span> NETLIB_MSG_READ<span class="token punctuation">,</span> <span class="token punctuation">(</span>net_handle_t<span class="token punctuation">)</span>m_socket<span class="token punctuation">,</span> <span class="token constant">NULL</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x56E0;&#x4E3A;&#x662F;&#x4FA6;&#x542C;socket&#xFF0C;&#x5176;&#x72B6;&#x6001;&#x88AB;&#x8BBE;&#x7F6E;&#x6210;SOCKET_STATE_LISTENING(&#x4E0A;&#x6587;&#x4ECB;&#x7ECD;&#x4E86;)&#x3002;&#x63A5;&#x7740;&#x5C31;&#x63A5;&#x53D7;&#x65B0;&#x8FDE;&#x63A5;&#x3002;</p>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">void</span> <span class="token class-name">CBaseSocket</span><span class="token operator">::</span><span class="token function">_AcceptNewSocket</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    SOCKET fd <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
    sockaddr_in peer_addr<span class="token punctuation">;</span>
    socklen_t addr_len <span class="token operator">=</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span>sockaddr_in<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">char</span> ip_str<span class="token punctuation">[</span><span class="token number">64</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
    <span class="token keyword">while</span> <span class="token punctuation">(</span> <span class="token punctuation">(</span>fd <span class="token operator">=</span> <span class="token function">accept</span><span class="token punctuation">(</span>m_socket<span class="token punctuation">,</span> <span class="token punctuation">(</span>sockaddr<span class="token operator">*</span><span class="token punctuation">)</span><span class="token operator">&amp;</span>peer_addr<span class="token punctuation">,</span> <span class="token operator">&amp;</span>addr_len<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> INVALID_SOCKET <span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
        CBaseSocket<span class="token operator">*</span> pSocket <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token function">CBaseSocket</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">uint32_t</span> ip <span class="token operator">=</span> <span class="token function">ntohl</span><span class="token punctuation">(</span>peer_addr<span class="token punctuation">.</span>sin_addr<span class="token punctuation">.</span>s_addr<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">uint16_t</span> port <span class="token operator">=</span> <span class="token function">ntohs</span><span class="token punctuation">(</span>peer_addr<span class="token punctuation">.</span>sin_port<span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token function">snprintf</span><span class="token punctuation">(</span>ip_str<span class="token punctuation">,</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span>ip_str<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">&quot;%d.%d.%d.%d&quot;</span><span class="token punctuation">,</span> ip <span class="token operator">&gt;&gt;</span> <span class="token number">24</span><span class="token punctuation">,</span> <span class="token punctuation">(</span>ip <span class="token operator">&gt;&gt;</span> <span class="token number">16</span><span class="token punctuation">)</span> <span class="token operator">&amp;</span> <span class="token number">0xFF</span><span class="token punctuation">,</span> <span class="token punctuation">(</span>ip <span class="token operator">&gt;&gt;</span> <span class="token number">8</span><span class="token punctuation">)</span> <span class="token operator">&amp;</span> <span class="token number">0xFF</span><span class="token punctuation">,</span> ip <span class="token operator">&amp;</span> <span class="token number">0xFF</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;AcceptNewSocket, socket=%d from %s:%d\n&quot;</span><span class="token punctuation">,</span> fd<span class="token punctuation">,</span> ip_str<span class="token punctuation">,</span> port<span class="token punctuation">)</span><span class="token punctuation">;</span>

        pSocket<span class="token operator">-&gt;</span><span class="token function">SetSocket</span><span class="token punctuation">(</span>fd<span class="token punctuation">)</span><span class="token punctuation">;</span>
        pSocket<span class="token operator">-&gt;</span><span class="token function">SetCallback</span><span class="token punctuation">(</span>m_callback<span class="token punctuation">)</span><span class="token punctuation">;</span>
        pSocket<span class="token operator">-&gt;</span><span class="token function">SetCallbackData</span><span class="token punctuation">(</span>m_callback_data<span class="token punctuation">)</span><span class="token punctuation">;</span>
        pSocket<span class="token operator">-&gt;</span><span class="token function">SetState</span><span class="token punctuation">(</span>SOCKET_STATE_CONNECTED<span class="token punctuation">)</span><span class="token punctuation">;</span>
        pSocket<span class="token operator">-&gt;</span><span class="token function">SetRemoteIP</span><span class="token punctuation">(</span>ip_str<span class="token punctuation">)</span><span class="token punctuation">;</span>
        pSocket<span class="token operator">-&gt;</span><span class="token function">SetRemotePort</span><span class="token punctuation">(</span>port<span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token function">_SetNoDelay</span><span class="token punctuation">(</span>fd<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">_SetNonblock</span><span class="token punctuation">(</span>fd<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">AddBaseSocket</span><span class="token punctuation">(</span>pSocket<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">CEventDispatch</span><span class="token operator">::</span><span class="token function">Instance</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-&gt;</span><span class="token function">AddEvent</span><span class="token punctuation">(</span>fd<span class="token punctuation">,</span> SOCKET_READ <span class="token operator">|</span> SOCKET_EXCEP<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">m_callback</span><span class="token punctuation">(</span>m_callback_data<span class="token punctuation">,</span> NETLIB_MSG_CONNECT<span class="token punctuation">,</span> <span class="token punctuation">(</span>net_handle_t<span class="token punctuation">)</span>fd<span class="token punctuation">,</span> <span class="token constant">NULL</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x63A5;&#x6536;&#x65B0;&#x8FDE;&#x63A5;&#xFF0C;&#x9700;&#x8981;&#x6CE8;&#x610F;&#x4EE5;&#x4E0B;&#x4E8B;&#x9879;&#xFF1A;</p>
<ol>
<li><p>&#x4EA7;&#x751F;&#x4E00;&#x4E2A;&#x65B0;&#x7684;socket&#x548C;&#x5BF9;&#x5E94;&#x7684;CBaseSocket&#x5BF9;&#x8C61;&#x3002;</p>
</li>
<li><p>&#x8BE5;socket&#x548C;&#x5BF9;&#x5E94;&#x7684;CBaseSocket&#x5BF9;&#x8C61;&#x548C;&#x4FA6;&#x542C;socket&#x4E00;&#x6837;&#x4E5F;&#x88AB;&#x52A0;&#x5165;&#x5168;&#x5C40;g_socket_map&#x4E2D;&#x8FDB;&#x884C;&#x7BA1;&#x7406;&#x3002;</p>
</li>
<li><p>&#x65B0;socket&#x540C;&#x6837;&#x88AB;&#x8BBE;&#x7F6E;&#x6210;&#x975E;&#x963B;&#x585E;&#x7684;&#x3002;</p>
</li>
<li><p>&#x7981;&#x7528;&#x8BE5;socket&#x7684;nagle&#x7B97;&#x6CD5;&#xFF08;_SetNoDelay(fd);&#xFF09;&#x3002;</p>
</li>
<li><p>&#x5173;&#x6CE8;&#x8BE5;socket&#x7684;&#x8BFB;&#x548C;&#x5F02;&#x5E38;&#x4E8B;&#x4EF6;&#xFF08;CEventDispatch::Instance()-&gt;AddEvent(fd, SOCKET_READ | SOCKET_EXCEP);&#xFF09;&#x3002;</p>
</li>
<li><p>&#x5C06;socket&#x7684;&#x72B6;&#x6001;&#x8BBE;&#x7F6E;&#x6210;SOCKET_STATE_CONNECTED&#x3002;</p>
</li>
<li><p>&#x8C03;&#x7528;&#x4FA6;&#x542C;socket&#x7684;&#x7684;&#x56DE;&#x8C03;&#x51FD;&#x6570;m_callback(m_callback_data, NETLIB_MSG_CONNECT, (net_handle_t)fd, NULL)&#xFF0C;&#x5E76;&#x4F20;&#x5165;&#x6D88;&#x606F;&#x7C7B;&#x578B;&#x662F;NETLIB_MSG_CONNECT&#x3002;</p>
</li>
</ol>
<p>&#x8FD9;&#x4E2A;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x5728;&#x4E0A;&#x9762;&#x521D;&#x59CB;&#x5316;&#x4FA6;&#x542C;&#x51FD;&#x6570;&#x8BBE;&#x7F6E;&#x7684;&#xFF0C;&#x6307;&#x5411;&#x51FD;&#x6570;proxy_serv_callback&#x3002;&#x8C03;&#x7528;&#x4EE3;&#x7801;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">void</span> <span class="token function">proxy_serv_callback</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token operator">*</span> callback_data<span class="token punctuation">,</span> <span class="token keyword">uint8_t</span> msg<span class="token punctuation">,</span> <span class="token keyword">uint32_t</span> handle<span class="token punctuation">,</span> <span class="token keyword">void</span><span class="token operator">*</span> pParam<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>msg <span class="token operator">==</span> NETLIB_MSG_CONNECT<span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
        CProxyConn<span class="token operator">*</span> pConn <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token function">CProxyConn</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        pConn<span class="token operator">-&gt;</span><span class="token function">OnConnect</span><span class="token punctuation">(</span>handle<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">else</span>
    <span class="token punctuation">{</span>
        <span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;!!!error msg: %d&quot;</span><span class="token punctuation">,</span> msg<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x63A5;&#x7740;&#x8C03;&#x7528;CProxyConn&#x7684;OnConnect()&#x51FD;&#x6570;&#xFF1A;</p>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">void</span> <span class="token class-name">CProxyConn</span><span class="token operator">::</span><span class="token function">OnConnect</span><span class="token punctuation">(</span>net_handle_t handle<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    m_handle <span class="token operator">=</span> handle<span class="token punctuation">;</span>

    g_proxy_conn_map<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span>handle<span class="token punctuation">,</span> <span class="token keyword">this</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token function">netlib_option</span><span class="token punctuation">(</span>handle<span class="token punctuation">,</span> NETLIB_OPT_SET_CALLBACK<span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token keyword">void</span><span class="token operator">*</span><span class="token punctuation">)</span>imconn_callback<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">netlib_option</span><span class="token punctuation">(</span>handle<span class="token punctuation">,</span> NETLIB_OPT_SET_CALLBACK_DATA<span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token keyword">void</span><span class="token operator">*</span><span class="token punctuation">)</span><span class="token operator">&amp;</span>g_proxy_conn_map<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">netlib_option</span><span class="token punctuation">(</span>handle<span class="token punctuation">,</span> NETLIB_OPT_GET_REMOTE_IP<span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token keyword">void</span><span class="token operator">*</span><span class="token punctuation">)</span><span class="token operator">&amp;</span>m_peer_ip<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">netlib_option</span><span class="token punctuation">(</span>handle<span class="token punctuation">,</span> NETLIB_OPT_GET_REMOTE_PORT<span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token keyword">void</span><span class="token operator">*</span><span class="token punctuation">)</span><span class="token operator">&amp;</span>m_peer_port<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;connect from %s:%d, handle=%d&quot;</span><span class="token punctuation">,</span> m_peer_ip<span class="token punctuation">.</span><span class="token function">c_str</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> m_peer_port<span class="token punctuation">,</span> m_handle<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x6CE8;&#x610F;&#xFF01;&#x8FD9;&#x91CC;&#xFF0C;&#x5DF2;&#x7ECF;&#x6084;&#x6084;&#x5730;&#x5C06;&#x8BE5;&#x65B0;socket&#x7684;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x7531;proxy_serv_callback&#x5077;&#x5077;&#x5730;&#x6362;&#x6210;&#x4E86;imconn_callback&#x3002;&#x540C;&#x65F6;&#xFF0C;&#x5C06;&#x8BE5;&#x8FDE;&#x63A5;&#x5BF9;&#x8C61;&#x653E;&#x5165;&#x4E00;&#x4E2A;&#x5168;&#x5C40;map g_proxy_conn_map&#x4E2D;&#xFF1A;</p>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">typedef</span> hash_map<span class="token operator">&lt;</span>net_handle_t<span class="token punctuation">,</span> CImConn<span class="token operator">*</span><span class="token operator">&gt;</span> ConnMap_t<span class="token punctuation">;</span>
</code></pre>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">static</span> ConnMap_t g_proxy_conn_map<span class="token punctuation">;</span>
</code></pre>
<p>&#x540C;&#x6837;&#xFF0C;&#x8BE5;map&#x7684;key&#x662F;socket&#x53E5;&#x67C4;&#xFF0C;value&#x662F;&#x8FDE;&#x63A5;&#x5BF9;&#x8C61;&#x57FA;&#x7C7B;&#x7684;&#x6307;&#x9488;&#x3002;</p>
<p>&#x81F3;&#x6B64;&#xFF0C;&#x5BF9;&#x4E8E;&#x4FA6;&#x542C;socket&#xFF0C;&#x5982;&#x679C;socket&#x53EF;&#x8BFB;&#xFF0C;&#x5219;&#x63A5;&#x6536;&#x65B0;&#x8FDE;&#x63A5;&#xFF0C;&#x5E76;&#x7F6E;&#x6362;&#x5176;&#x9ED8;&#x8BA4;OnRead&#x7684;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x4E3A;imconn_callback&#xFF1B;&#x800C;&#x5BF9;&#x4E8E;&#x65B0;socket&#xFF0C;&#x5982;&#x679C;socket&#x53EF;&#x8BFB;&#xFF0C;&#x5219;&#x4F1A;&#x8C03;&#x7528;imconn_callback&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x63A5;&#x7740;&#x770B;&#x65B0;socket&#x53EF;&#x8BFB;&#x7684;&#x5904;&#x7406;&#x6D41;&#x7A0B;&#xFF1A;</p>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">void</span> <span class="token class-name">CBaseSocket</span><span class="token operator">::</span><span class="token function">OnRead</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>m_state <span class="token operator">==</span> SOCKET_STATE_LISTENING<span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
        <span class="token function">_AcceptNewSocket</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">else</span>
    <span class="token punctuation">{</span>
        u_long avail <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span> <span class="token punctuation">(</span><span class="token function">ioctlsocket</span><span class="token punctuation">(</span>m_socket<span class="token punctuation">,</span> FIONREAD<span class="token punctuation">,</span> <span class="token operator">&amp;</span>avail<span class="token punctuation">)</span> <span class="token operator">==</span> SOCKET_ERROR<span class="token punctuation">)</span> <span class="token operator">||</span> <span class="token punctuation">(</span>avail <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">)</span>
        <span class="token punctuation">{</span>
            <span class="token function">m_callback</span><span class="token punctuation">(</span>m_callback_data<span class="token punctuation">,</span> NETLIB_MSG_CLOSE<span class="token punctuation">,</span> <span class="token punctuation">(</span>net_handle_t<span class="token punctuation">)</span>m_socket<span class="token punctuation">,</span> <span class="token constant">NULL</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">else</span>
        <span class="token punctuation">{</span>
            <span class="token function">m_callback</span><span class="token punctuation">(</span>m_callback_data<span class="token punctuation">,</span> NETLIB_MSG_READ<span class="token punctuation">,</span> <span class="token punctuation">(</span>net_handle_t<span class="token punctuation">)</span>m_socket<span class="token punctuation">,</span> <span class="token constant">NULL</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x4E0A;&#x8FF0;OnRead&#x51FD;&#x6570;&#x4F1A;&#x8D70;else&#x5206;&#x652F;&#xFF0C;&#x5148;&#x8C03;&#x7528;ioctlsocket&#x83B7;&#x5F97;&#x53EF;&#x8BFB;&#x7684;&#x6570;&#x636E;&#x5B57;&#x8282;&#x6570;&#x3002;&#x5982;&#x679C;&#x51FA;&#x9519;&#x6216;&#x8005;&#x5B57;&#x8282;&#x6570;&#x4E3A;0&#xFF0C;&#x5219;&#x4EE5;&#x6D88;&#x606F;NETLIB_MSG_CLOSE&#x8C03;&#x7528;&#x56DE;&#x8C03;&#x51FD;&#x6570;imconn_callback,</p>
<p>&#x53CD;&#x4E4B;&#xFF0C;&#x4EE5;&#x6D88;&#x606F;NETLIB_MSG_READ&#x8C03;&#x7528;&#x56DE;&#x8C03;&#x51FD;&#x6570;imconn_callback&#x3002;</p>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">void</span> <span class="token function">imconn_callback</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token operator">*</span> callback_data<span class="token punctuation">,</span> <span class="token keyword">uint8_t</span> msg<span class="token punctuation">,</span> <span class="token keyword">uint32_t</span> handle<span class="token punctuation">,</span> <span class="token keyword">void</span><span class="token operator">*</span> pParam<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token function">NOTUSED_ARG</span><span class="token punctuation">(</span>handle<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">NOTUSED_ARG</span><span class="token punctuation">(</span>pParam<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>callback_data<span class="token punctuation">)</span>
        <span class="token keyword">return</span><span class="token punctuation">;</span>

    ConnMap_t<span class="token operator">*</span> conn_map <span class="token operator">=</span> <span class="token punctuation">(</span>ConnMap_t<span class="token operator">*</span><span class="token punctuation">)</span>callback_data<span class="token punctuation">;</span>
    CImConn<span class="token operator">*</span> pConn <span class="token operator">=</span> <span class="token function">FindImConn</span><span class="token punctuation">(</span>conn_map<span class="token punctuation">,</span> handle<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>pConn<span class="token punctuation">)</span>
        <span class="token keyword">return</span><span class="token punctuation">;</span>

    <span class="token comment">//log(&quot;msg=%d, handle=%d &quot;, msg, handle);</span>

    <span class="token keyword">switch</span> <span class="token punctuation">(</span>msg<span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
    <span class="token keyword">case</span> NETLIB_MSG_CONFIRM<span class="token operator">:</span>
        pConn<span class="token operator">-&gt;</span><span class="token function">OnConfirm</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">break</span><span class="token punctuation">;</span>
    <span class="token keyword">case</span> NETLIB_MSG_READ<span class="token operator">:</span>
        pConn<span class="token operator">-&gt;</span><span class="token function">OnRead</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">break</span><span class="token punctuation">;</span>
    <span class="token keyword">case</span> NETLIB_MSG_WRITE<span class="token operator">:</span>
        pConn<span class="token operator">-&gt;</span><span class="token function">OnWrite</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">break</span><span class="token punctuation">;</span>
    <span class="token keyword">case</span> NETLIB_MSG_CLOSE<span class="token operator">:</span>
        pConn<span class="token operator">-&gt;</span><span class="token function">OnClose</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">break</span><span class="token punctuation">;</span>
    <span class="token keyword">default</span><span class="token operator">:</span>
        <span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;!!!imconn_callback error msg: %d &quot;</span><span class="token punctuation">,</span> msg<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">break</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    pConn<span class="token operator">-&gt;</span><span class="token function">ReleaseRef</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x51FA;&#x9519;&#x6D88;&#x606F;NETLIB_MSG_CLOSE&#x6CA1;&#x5565;&#x597D;&#x770B;&#x7684;&#xFF0C;&#x65E0;&#x975E;&#x662F;&#x5173;&#x95ED;&#x8FDE;&#x63A5;&#x3002;&#x6211;&#x4EEC;&#x6765;&#x770B;NETLIB_MSG_READ&#x6D88;&#x606F;&#xFF0C;&#x4F1A;&#x8C03;&#x7528;pConn-&gt;OnRead()&#xFF0C;pConn&#x662F;&#x4E00;&#x4E2A;CImConn&#x6307;&#x9488;&#xFF0C;&#x4F46;&#x6839;&#x636E;&#x4E0A;&#x6587;&#x4ECB;&#x7ECD;&#x6211;&#x4EEC;&#x77E5;&#x9053;&#xFF0C;&#x5176;&#x5B9E;&#x9645;&#x662F;&#x4E00;&#x4E2A;CImConn&#x7684;&#x5B50;&#x7C7B;CProxyConn&#x5BF9;&#x8C61;&#xFF1A;</p>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">class</span> <span class="token class-name">CProxyConn</span> <span class="token operator">:</span> <span class="token base-clause"><span class="token keyword">public</span> <span class="token class-name">CImConn</span></span> <span class="token punctuation">{</span>
</code></pre>
<p>&#x6240;&#x4EE5;pConn-&gt;OnRead()&#x5B9E;&#x9645;&#x4F1A;&#x8C03;&#x7528;CProxyConn&#x7684;OnRead()&#xFF1A;</p>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">void</span> <span class="token class-name">CProxyConn</span><span class="token operator">::</span><span class="token function">OnRead</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token punctuation">;</span><span class="token punctuation">;</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">uint32_t</span> free_buf_len <span class="token operator">=</span> m_in_buf<span class="token punctuation">.</span><span class="token function">GetAllocSize</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span> m_in_buf<span class="token punctuation">.</span><span class="token function">GetWriteOffset</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>free_buf_len <span class="token operator">&lt;</span> READ_BUF_SIZE<span class="token punctuation">)</span>
            m_in_buf<span class="token punctuation">.</span><span class="token function">Extend</span><span class="token punctuation">(</span>READ_BUF_SIZE<span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token keyword">int</span> ret <span class="token operator">=</span> <span class="token function">netlib_recv</span><span class="token punctuation">(</span>m_handle<span class="token punctuation">,</span> m_in_buf<span class="token punctuation">.</span><span class="token function">GetBuffer</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> m_in_buf<span class="token punctuation">.</span><span class="token function">GetWriteOffset</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> READ_BUF_SIZE<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>ret <span class="token operator">&lt;=</span> <span class="token number">0</span><span class="token punctuation">)</span>
            <span class="token keyword">break</span><span class="token punctuation">;</span>

        m_recv_bytes <span class="token operator">+=</span> ret<span class="token punctuation">;</span>
        m_in_buf<span class="token punctuation">.</span><span class="token function">IncWriteOffset</span><span class="token punctuation">(</span>ret<span class="token punctuation">)</span><span class="token punctuation">;</span>
        m_last_recv_tick <span class="token operator">=</span> <span class="token function">get_tick_count</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">uint32_t</span> pdu_len <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
    <span class="token keyword">try</span> <span class="token punctuation">{</span>
        <span class="token keyword">while</span> <span class="token punctuation">(</span> <span class="token class-name">CImPdu</span><span class="token operator">::</span><span class="token function">IsPduAvailable</span><span class="token punctuation">(</span>m_in_buf<span class="token punctuation">.</span><span class="token function">GetBuffer</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> m_in_buf<span class="token punctuation">.</span><span class="token function">GetWriteOffset</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> pdu_len<span class="token punctuation">)</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token function">HandlePduBuf</span><span class="token punctuation">(</span>m_in_buf<span class="token punctuation">.</span><span class="token function">GetBuffer</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> pdu_len<span class="token punctuation">)</span><span class="token punctuation">;</span>
            m_in_buf<span class="token punctuation">.</span><span class="token function">Read</span><span class="token punctuation">(</span><span class="token constant">NULL</span><span class="token punctuation">,</span> pdu_len<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span>CPduException<span class="token operator">&amp;</span> ex<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;!!!catch exception, err_code=%u, err_msg=%s, close the connection &quot;</span><span class="token punctuation">,</span>
            ex<span class="token punctuation">.</span><span class="token function">GetErrorCode</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> ex<span class="token punctuation">.</span><span class="token function">GetErrorMsg</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">OnClose</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

<span class="token punctuation">}</span>
</code></pre>
<p>CImConn&#x5B9E;&#x9645;&#x662F;&#x4EE3;&#x8868;&#x4E00;&#x4E2A;&#x8FDE;&#x63A5;&#xFF0C;&#x5373;&#x6BCF;&#x4E00;&#x4E2A;&#x8FDE;&#x63A5;&#x90FD;&#x6709;&#x8FD9;&#x6837;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x3002;&#x5177;&#x4F53;&#x88AB;&#x5206;&#x5316;&#x6210;&#x5B83;&#x7684;&#x5404;&#x4E2A;&#x5B50;&#x5BF9;&#x8C61;&#xFF0C;&#x5982;CProxyConn&#x3002;&#x6BCF;&#x4E00;&#x4E2A;&#x8FDE;&#x63A5;CImConn&#x90FD;&#x5B58;&#x5728;&#x4E00;&#x4E2A;&#x8BFB;&#x7F13;&#x51B2;&#x533A;&#x548C;&#x5199;&#x7F13;&#x51B2;&#x533A;&#xFF0C;&#x8BFB;&#x7F13;&#x51B2;&#x533A;&#x7528;&#x4E8E;&#x5B58;&#x653E;&#x4ECE;&#x7F51;&#x7EDC;&#x4E0A;&#x6536;&#x53D6;&#x7684;&#x6570;&#x636E;&#xFF0C;&#x5199;&#x7F13;&#x51B2;&#x533A;&#x7528;&#x4E8E;&#x5B58;&#x653E;&#x5373;&#x5C06;&#x53D1;&#x5230;&#x7F51;&#x7EDC;&#x4E2D;&#x6570;&#x636E;&#x3002;CProxyConn::OnRead()&#x5148;&#x68C0;&#x6D4B;&#x8BE5;&#x5BF9;&#x8C61;&#x7684;&#x8BFB;&#x7F13;&#x51B2;&#x533A;&#x4E2D;&#x8FD8;&#x6709;&#x591A;&#x5C11;&#x53EF;&#x7528;&#x7A7A;&#x95F4;&#xFF0C;&#x5982;&#x679C;&#x53EF;&#x7528;&#x7A7A;&#x95F4;&#x5C0F;&#x4E8E;&#x5F53;&#x524D;&#x6536;&#x5230;&#x7684;&#x5B57;&#x8282;&#x6570;&#x76EE;&#xFF0C;&#x5219;&#x5C06;&#x8BE5;&#x8BFB;&#x7F13;&#x51B2;&#x533A;&#x7684;&#x5927;&#x5C0F;&#x6269;&#x5C55;&#x5230;&#x9700;&#x8981;&#x7684;&#x5927;&#x5C0F;READ_BUF_SIZE&#x3002;&#x63A5;&#x7740;&#x6536;&#x5230;&#x7684;&#x6570;&#x636E;&#x653E;&#x5165;&#x8BFB;&#x7F13;&#x51B2;&#x533A;&#x4E2D;&#x3002;&#x5E76;&#x8BB0;&#x5F55;&#x4E0B;&#x8FD9;&#x6B21;&#x6536;&#x53D6;&#x6570;&#x636E;&#x7684;&#x65F6;&#x95F4;&#x5230;m_last_recv_tick&#x53D8;&#x91CF;&#x4E2D;&#x3002;&#x63A5;&#x7740;&#x5F00;&#x59CB;&#x89E3;&#x5305;&#xFF0C;&#x5373;&#x8C03;&#x7528;CImPdu::IsPduAvailable()&#x4ECE;&#x8BFB;&#x53D6;&#x7F13;&#x51B2;&#x533A;&#x4E2D;&#x53D6;&#x51FA;&#x6570;&#x636E;&#x5904;&#x7406;&#xFF0C;&#x5148;&#x5224;&#x65AD;&#x73B0;&#x6709;&#x6570;&#x636E;&#x662F;&#x5426;&#x5927;&#x4E8E;&#x4E00;&#x4E2A;&#x5305;&#x5934;&#x7684;&#x5927;&#x5C0F;&#xFF0C;&#x5982;&#x679C;&#x4E0D;&#x5927;&#x4E8E;&#xFF0C;&#x9000;&#x51FA;&#x3002;&#x5982;&#x679C;&#x5927;&#x4E8E;&#x4E00;&#x4E2A;&#x5305;&#x5934;&#x7684;&#x5927;&#x5C0F;&#xFF0C;&#x5219;&#x63A5;&#x7740;&#x6839;&#x636E;&#x5305;&#x5934;&#x4E2D;&#x7684;&#x4FE1;&#x606F;&#x5F97;&#x5230;&#x6574;&#x4E2A;&#x5305;&#x7684;&#x5927;&#x5C0F;&#xFF1A;</p>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">bool</span> <span class="token class-name">CImPdu</span><span class="token operator">::</span><span class="token function">IsPduAvailable</span><span class="token punctuation">(</span>uchar_t<span class="token operator">*</span> buf<span class="token punctuation">,</span> <span class="token keyword">uint32_t</span> len<span class="token punctuation">,</span> <span class="token keyword">uint32_t</span><span class="token operator">&amp;</span> pdu_len<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>len <span class="token operator">&lt;</span> IM_PDU_HEADER_LEN<span class="token punctuation">)</span>
        <span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span>

    pdu_len <span class="token operator">=</span> <span class="token class-name">CByteStream</span><span class="token operator">::</span><span class="token function">ReadUint32</span><span class="token punctuation">(</span>buf<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>pdu_len <span class="token operator">&gt;</span> len<span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
        <span class="token comment">//log(&quot;pdu_len=%d, len=%d\n&quot;, pdu_len, len);</span>
        <span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token number">0</span> <span class="token operator">==</span> pdu_len<span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
        <span class="token keyword">throw</span> <span class="token function">CPduException</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">&quot;pdu_len is 0&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">return</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x5F97;&#x5230;&#x5305;&#x7684;&#x5927;&#x5C0F;&#x5C31;&#x53EF;&#x4EE5;&#x6B63;&#x5F0F;&#x5904;&#x7406;&#x5305;&#x4E86;&#xFF0C;&#x8C03;&#x7528;HandlePduBuf(m_in_buf.GetBuffer(), pdu_len);</p>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">void</span> <span class="token class-name">CProxyConn</span><span class="token operator">::</span><span class="token function">HandlePduBuf</span><span class="token punctuation">(</span>uchar_t<span class="token operator">*</span> pdu_buf<span class="token punctuation">,</span> <span class="token keyword">uint32_t</span> pdu_len<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    CImPdu<span class="token operator">*</span> pPdu <span class="token operator">=</span> <span class="token constant">NULL</span><span class="token punctuation">;</span>
    pPdu <span class="token operator">=</span> <span class="token class-name">CImPdu</span><span class="token operator">::</span><span class="token function">ReadPdu</span><span class="token punctuation">(</span>pdu_buf<span class="token punctuation">,</span> pdu_len<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>pPdu<span class="token operator">-&gt;</span><span class="token function">GetCommandId</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> IM<span class="token operator">::</span>BaseDefine<span class="token operator">::</span>CID_OTHER_HEARTBEAT<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    pdu_handler_t handler <span class="token operator">=</span> s_handler_map<span class="token operator">-&gt;</span><span class="token function">GetHandler</span><span class="token punctuation">(</span>pPdu<span class="token operator">-&gt;</span><span class="token function">GetCommandId</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span>handler<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        CTask<span class="token operator">*</span> pTask <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token function">CProxyTask</span><span class="token punctuation">(</span>m_uuid<span class="token punctuation">,</span> handler<span class="token punctuation">,</span> pPdu<span class="token punctuation">)</span><span class="token punctuation">;</span>
        g_thread_pool<span class="token punctuation">.</span><span class="token function">AddTask</span><span class="token punctuation">(</span>pTask<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
        <span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;no handler for packet type: %d&quot;</span><span class="token punctuation">,</span> pPdu<span class="token operator">-&gt;</span><span class="token function">GetCommandId</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x5305;&#x7684;&#x6570;&#x636E;&#x7ED3;&#x6784;&#x662F;CImPdu&#xFF08;Im &#x5373;Instant Message&#x5373;&#x65F6;&#x901A;&#x8BAF;&#x8F6F;&#x4EF6;&#x7684;&#x610F;&#x601D;&#xFF0C;teamtalk&#x672C;&#x6765;&#x5C31;&#x662F;&#x4E00;&#x6B3E;&#x5373;&#x65F6;&#x901A;&#x8BAF;&#xFF0C;pdu&#xFF0C;Protocol Data Unit &#x534F;&#x8BAE;&#x6570;&#x636E;&#x5355;&#x5143;&#xFF0C;&#x901A;&#x4FD7;&#x7684;&#x8BF4;&#x5C31;&#x662F;&#x4E00;&#x4E2A;&#x5305;&#x5355;&#x4F4D;&#xFF09;&#xFF0C;&#x8BE5;&#x6570;&#x636E;&#x7ED3;&#x6784;&#x5206;&#x4E3A;&#x5305;&#x5934;&#x548C;&#x5305;&#x4F53;&#x4E24;&#x90E8;&#x5206;&#x3002;&#x7C7B;CImPdu&#x7684;&#x4E24;&#x4E2A;&#x6210;&#x5458;&#x53D8;&#x91CF;&#xFF1A;</p>
<pre class="language-"><code class="lang-cpp">CSimpleBuffer    m_buf<span class="token punctuation">;</span>
PduHeader_t      m_pdu_header<span class="token punctuation">;</span>
</code></pre>
<p>&#x5206;&#x522B;&#x8868;&#x793A;&#x5305;&#x5934;&#x548C;&#x5305;&#x4F53;&#xFF0C;&#x5305;&#x5934;&#x7684;&#x5B9A;&#x4E49;PduHeader_t&#x5982;&#x4E0B;&#xFF1A;</p>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">typedef</span> <span class="token keyword">struct</span> <span class="token punctuation">{</span>
    <span class="token keyword">uint32_t</span>     length<span class="token punctuation">;</span>          <span class="token comment">// the whole pdu length</span>
    <span class="token keyword">uint16_t</span>     version<span class="token punctuation">;</span>      <span class="token comment">// pdu version number</span>
    <span class="token keyword">uint16_t</span>    flag<span class="token punctuation">;</span>          <span class="token comment">// not used</span>
    <span class="token keyword">uint16_t</span>    service_id<span class="token punctuation">;</span>      <span class="token comment">//</span>
    <span class="token keyword">uint16_t</span>    command_id<span class="token punctuation">;</span>      <span class="token comment">//</span>
    <span class="token keyword">uint16_t</span>    seq_num<span class="token punctuation">;</span>     <span class="token comment">// &#x5305;&#x5E8F;&#x53F7;</span>
    <span class="token keyword">uint16_t</span>    reversed<span class="token punctuation">;</span>    <span class="token comment">// &#x4FDD;&#x7559;</span>
<span class="token punctuation">}</span> PduHeader_t<span class="token punctuation">;</span>
</code></pre>
<p>&#x901A;&#x8FC7;&#x5305;&#x5934;&#x7684;command_id&#x5C31;&#x77E5;&#x9053;&#x8BE5;&#x5305;&#x662F;&#x4EC0;&#x4E48;&#x6570;&#x636E;&#x4E86;&#x3002;&#x63A5;&#x7740;&#x6839;&#x636E;&#x5BF9;&#x5E94;&#x7684;&#x547D;&#x4EE4;&#x53F7;&#x8C03;&#x7528;&#x5728;&#x7A0B;&#x5E8F;&#x521D;&#x59CB;&#x5316;&#x9636;&#x6BB5;&#x7ED1;&#x5B9A;&#x7684;&#x5305;&#x5904;&#x7406;&#x51FD;&#x6570;&#xFF1A;</p>
<pre class="language-"><code class="lang-cpp">pdu_handler_t handler <span class="token operator">=</span> s_handler_map<span class="token operator">-&gt;</span><span class="token function">GetHandler</span><span class="token punctuation">(</span>pPdu<span class="token operator">-&gt;</span><span class="token function">GetCommandId</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
<p>&#x6267;&#x884C;&#x5904;&#x7406;&#x51FD;&#x6570;&#x4E0D;&#x662F;&#x76F4;&#x63A5;&#x8C03;&#x7528;&#x8BE5;&#x51FD;&#x6570;&#xFF0C;&#x800C;&#x662F;&#x5305;&#x88C5;&#x6210;&#x4E00;&#x4E2A;&#x4EFB;&#x52A1;&#x653E;&#x5165;&#x524D;&#x9762;&#x4ECB;&#x7ECD;&#x7684;&#x4EFB;&#x52A1;&#x961F;&#x5217;&#x4E2D;&#xFF1A;</p>
<pre class="language-"><code class="lang-cpp">du_handler_t handler <span class="token operator">=</span> s_handler_map<span class="token operator">-&gt;</span><span class="token function">GetHandler</span><span class="token punctuation">(</span>pPdu<span class="token operator">-&gt;</span><span class="token function">GetCommandId</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span>handler<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        CTask<span class="token operator">*</span> pTask <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token function">CProxyTask</span><span class="token punctuation">(</span>m_uuid<span class="token punctuation">,</span> handler<span class="token punctuation">,</span> pPdu<span class="token punctuation">)</span><span class="token punctuation">;</span>
        g_thread_pool<span class="token punctuation">.</span><span class="token function">AddTask</span><span class="token punctuation">(</span>pTask<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
        <span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;no handler for packet type: %d&quot;</span><span class="token punctuation">,</span> pPdu<span class="token operator">-&gt;</span><span class="token function">GetCommandId</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
</code></pre>
<p>&#x524D;&#x9762;&#x4ECB;&#x7ECD;&#x8FC7;&#xFF0C;&#x5904;&#x7406;&#x4EFB;&#x52A1;&#x7684;&#x7EBF;&#x7A0B;&#x53EF;&#x80FD;&#x6709;&#x591A;&#x4E2A;&#xFF0C;&#x90A3;&#x4E48;&#x5230;&#x5E95;&#x5C06;&#x4EFB;&#x52A1;&#x52A0;&#x5165;&#x5230;&#x54EA;&#x4E2A;&#x5DE5;&#x4F5C;&#x7EBF;&#x7A0B;&#x5462;&#xFF1F;&#x8FD9;&#x91CC;&#x91C7;&#x53D6;&#x7684;&#x7B56;&#x7565;&#x662F;&#x968F;&#x673A;&#x5206;&#x914D;&#xFF1A;</p>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">void</span> <span class="token class-name">CThreadPool</span><span class="token operator">::</span><span class="token function">AddTask</span><span class="token punctuation">(</span>CTask<span class="token operator">*</span> pTask<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token comment">/*
     * select a random thread to push task
     * we can also select a thread that has less task to do
     * but that will scan the whole thread list and use thread lock to get each task size
     */</span>
    <span class="token keyword">uint32_t</span> thread_idx <span class="token operator">=</span> <span class="token function">random</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">%</span> m_worker_size<span class="token punctuation">;</span>
    m_worker_list<span class="token punctuation">[</span>thread_idx<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">PushTask</span><span class="token punctuation">(</span>pTask<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x5F53;&#x7136;&#x9700;&#x8981;&#x6CE8;&#x610F;&#x7684;&#x662F;&#x3002;&#x5982;&#x679C;&#x6570;&#x636E;&#x5305;&#x662F;&#x5FC3;&#x8DF3;&#x5305;&#x7684;&#x8BDD;&#xFF0C;&#x5C31;&#x76F4;&#x63A5;&#x4E0D;&#x5904;&#x7406;&#x4E86;&#x3002;&#x56E0;&#x4E3A;&#x5FC3;&#x8DF3;&#x5305;&#x53EA;&#x662F;&#x6765;&#x4FDD;&#x6D3B;&#x901A;&#x4FE1;&#x7684;&#xFF0C;&#x4E0E;&#x5177;&#x4F53;&#x4E1A;&#x52A1;&#x65E0;&#x5173;&#xFF1A;</p>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">if</span> <span class="token punctuation">(</span>pPdu<span class="token operator">-&gt;</span><span class="token function">GetCommandId</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> IM<span class="token operator">::</span>BaseDefine<span class="token operator">::</span>CID_OTHER_HEARTBEAT<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
</code></pre>
<p>&#x8BE5;&#x5305;&#x5904;&#x7406;&#x5B8C;&#x6210;&#x4EE5;&#x540E;&#xFF0C;&#x5C06;&#x8BE5;&#x5305;&#x7684;&#x6570;&#x636E;&#x4ECE;&#x8FDE;&#x63A5;&#x7684;&#x8BFB;&#x7F13;&#x51B2;&#x533A;&#x79FB;&#x9664;&#xFF1A;</p>
<pre class="language-"><code class="lang-cpp">m_in_buf<span class="token punctuation">.</span><span class="token function">Read</span><span class="token punctuation">(</span><span class="token constant">NULL</span><span class="token punctuation">,</span> pdu_len<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
<p>&#x63A5;&#x7740;&#x7EE7;&#x7EED;&#x5904;&#x7406;&#x4E0B;&#x4E00;&#x4E2A;&#x5305;&#xFF0C;&#x56E0;&#x4E3A;&#x6536;&#x6765;&#x7684;&#x6570;&#x636E;&#x53EF;&#x80FD;&#x4E0D;&#x591F;&#x4E00;&#x4E2A;&#x5305;&#x5927;&#x5C0F;&#xFF0C;&#x4E5F;&#x53EF;&#x80FD;&#x662F;&#x591A;&#x4E2A;&#x5305;&#x7684;&#x5927;&#x5C0F;&#xFF0C;&#x6240;&#x4EE5;&#x8981;&#x653E;&#x5728;&#x4E00;&#x4E2A;&#x5FAA;&#x73AF;&#x91CC;&#x9762;&#x89E3;&#x5305;&#x5904;&#x7406;&#xFF0C;&#x76F4;&#x5230;&#x8BFB;&#x7F13;&#x51B2;&#x533A;&#x4E2D;&#x65E0;&#x6570;&#x636E;&#x6216;&#x6570;&#x636E;&#x4E0D;&#x591F;&#x4E00;&#x4E2A;&#x5305;&#x7684;&#x5927;&#x5C0F;&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x5C06;&#x8FD9;&#x4E2A;&#x6D41;&#x7A0B;&#x62BD;&#x8C61;&#x51FA;&#x6765;&#xFF0C;&#x8FD9;&#x4E2A;&#x6D41;&#x7A0B;&#x4E5F;&#x662F;&#x73B0;&#x5728;&#x6240;&#x6709;&#x7F51;&#x7EDC;&#x901A;&#x4FE1;&#x5E93;&#x90FD;&#x8981;&#x505A;&#x7684;&#x5DE5;&#x4F5C;&#xFF1A;</p>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">while</span><span class="token punctuation">(</span>&#x9000;&#x51FA;&#x6761;&#x4EF6;<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token comment">//1. &#x68C0;&#x6D4B;&#x975E;&#x4FA6;&#x542C;socket&#x53EF;&#x8BFB;</span>

    <span class="token comment">//2. &#x5904;&#x7406;&#x53EF;&#x8BFB;&#x4E8B;&#x4EF6;</span>

    <span class="token comment">//3. &#x68C0;&#x6D4B;&#x53EF;&#x8BFB;&#x53D6;&#x7684;&#x5B57;&#x8282;&#x6570;&#xFF0C;&#x51FA;&#x9519;&#x5C31;&#x5173;&#x95ED;&#xFF0C;&#x4E0D;&#x51FA;&#x9519;&#xFF0C;&#x5C06;&#x6536;&#x53D6;&#x7684;&#x5B57;&#x8282;&#x653E;&#x5165;&#x8FDE;&#x63A5;&#x7684;&#x8BFB;&#x7F13;&#x51B2;&#x533A;</span>

    <span class="token comment">//&#x5FAA;&#x73AF;&#x505A;&#x4EE5;&#x4E0B;&#x5904;&#x7406;</span>
    <span class="token comment">//4. &#x68C0;&#x6D4B;&#x53EF;&#x8BFB;&#x7F13;&#x51B2;&#x533A;&#x6570;&#x636E;&#x5927;&#x5C0F;&#x662F;&#x5426;&#x5927;&#x4E8E;&#x7B49;&#x4E8E;&#x4E00;&#x4E2A;&#x5305;&#x5934;&#x5927;&#x5C0F;: &#x5426;&#xFF0C;&#x6570;&#x636E;&#x4E0D;&#x591F;&#x4E00;&#x4E2A;&#x5305;&#xFF0C;&#x8DF3;&#x51FA;&#x8BE5;&#x5FAA;&#x73AF;;</span>
    <span class="token comment">//      &#x662F;&#xFF0C;&#x4ECE;&#x5305;&#x5934;&#x4E2D;&#x5F97;&#x5230;&#x4E00;&#x4E2A;&#x5305;&#x4F53;&#x7684;&#x5927;&#x5C0F;&#xFF0C;&#x68C0;&#x6D4B;&#x8BFB;&#x7F13;&#x51B2;&#x533A;&#x662F;&#x5426;&#x591F;&#x4E00;&#x4E2A;&#x5305;&#x5934;+&#x5305;&#x4F53;&#x7684;&#x5927;&#x5C0F;&#xFF1B;&#x5426;&#xFF0C;&#x6570;&#x636E;&#x4E0D;&#x591F;&#x4E00;&#x4E2A;&#x5305;&#xFF0C;&#x8DF3;&#x51FA;&#x5FAA;&#x73AF;</span>
    <span class="token comment">//      &#x662F;&#xFF0C;&#x89E3;&#x5305;&#xFF0C;&#x6839;&#x636E;&#x5305;&#x547D;&#x4EE4;&#x53F7;&#xFF0C;&#x5904;&#x7406;&#x8BE5;&#x5305;&#x6570;&#x636E;&#xFF0C;&#x53EF;&#x4EE5;&#x4EA7;&#x751F;&#x4E00;&#x4E2A;&#x4EFB;&#x52A1;&#xFF0C;&#x4E22;&#x5165;&#x4EFB;&#x52A1;&#x961F;&#x5217;&#x3002;</span>
    <span class="token comment">//      &#x4ECE;&#x53EF;&#x8BFB;&#x7F13;&#x51B2;&#x533A;&#x4E2D;&#x79FB;&#x9664;&#x521A;&#x624D;&#x5904;&#x7406;&#x7684;&#x5305;&#x6570;&#x636E;&#x7684;&#x5B57;&#x8282;&#x6570;&#x76EE;&#x3002;</span>
    <span class="token comment">//      &#x7EE7;&#x7EED;&#x7B2C;4&#x6B65;&#x3002;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x5F53;&#x52A0;&#x5165;&#x4EFB;&#x52A1;&#x540E;&#xFF0C;&#x4EFB;&#x52A1;&#x961F;&#x5217;&#x7EBF;&#x7A0B;&#x88AB;&#x5524;&#x9192;&#xFF0C;&#x4ECE;&#x4EFB;&#x52A1;&#x961F;&#x5217;&#x7684;&#x5934;&#x90E8;&#x62FF;&#x51FA;&#x8BE5;&#x4EFB;&#x52A1;&#x6267;&#x884C;&#x3002;&#x8FD9;&#x4E2A;&#x4E0A;&#x6587;&#x4ECB;&#x7ECD;&#x8FC7;&#x4E86;&#x3002;</p>
<p>&#x5230;&#x6B64;&#xFF0C;&#x672C;&#x6587;&#x8FD8;&#x6CA1;&#x6709;&#x5B8C;&#xFF0C;&#x56E0;&#x4E3A;&#x4E0A;&#x6587;&#x53EA;&#x4ECB;&#x7ECD;&#x4E86;&#x4ECE;&#x5BA2;&#x6237;&#x7AEF;&#x6536;&#x53D6;&#x6570;&#x636E;&#xFF0C;&#x7136;&#x540E;&#x89E3;&#x5305;&#x3002;&#x5E76;&#x6CA1;&#x6709;&#x4ECB;&#x7ECD;&#x89E3;&#x5B8C;&#x5305;&#xFF0C;&#x8C03;&#x7528;&#x5904;&#x7406;&#x51FD;&#x6570;&#x5904;&#x7406;&#x540E;&#x5982;&#x4F55;&#x5E94;&#x7B54;&#x5BA2;&#x6237;&#x7AEF;&#x3002;&#x4E0B;&#x9762;&#x4EE5;&#x4E00;&#x4E2A;&#x767B;&#x5F55;&#x6570;&#x636E;&#x5305;&#x7684;&#x5E94;&#x7B54;&#x6765;&#x53D9;&#x8FF0;&#x8FD9;&#x4E2A;&#x5E94;&#x7B54;&#x6D41;&#x7A0B;&#x3002;&#x767B;&#x5F55;&#x4EFB;&#x52A1;&#x4ECE;&#x4EFB;&#x52A1;&#x961F;&#x5217;&#x4E2D;&#x53D6;&#x51FA;&#x6765;&#x540E;&#xFF0C;&#x6267;&#x884C;&#x5982;&#x4E0B;&#x51FD;&#x6570;&#xFF1A;</p>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">void</span> <span class="token class-name">CHandlerMap</span><span class="token operator">::</span><span class="token function">Init</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token comment">//DB_PROXY&#x662F;&#x547D;&#x540D;&#x7A7A;&#x95F4;&#xFF0C;&#x4E0D;&#x662F;&#x7C7B;&#x540D;</span>
    <span class="token comment">// Login validate</span>
    m_handler_map<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span><span class="token keyword">uint32_t</span><span class="token punctuation">(</span>CID_OTHER_VALIDATE_REQ<span class="token punctuation">)</span><span class="token punctuation">,</span> DB_PROXY<span class="token operator">::</span>doLogin<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">void</span> <span class="token function">doLogin</span><span class="token punctuation">(</span>CImPdu<span class="token operator">*</span> pPdu<span class="token punctuation">,</span> <span class="token keyword">uint32_t</span> conn_uuid<span class="token punctuation">)</span>
<span class="token punctuation">{</span>

    CImPdu<span class="token operator">*</span> pPduResp <span class="token operator">=</span> <span class="token keyword">new</span> CImPdu<span class="token punctuation">;</span>

    IM<span class="token operator">::</span>Server<span class="token operator">::</span>IMValidateReq msg<span class="token punctuation">;</span>
    IM<span class="token operator">::</span>Server<span class="token operator">::</span>IMValidateRsp msgResp<span class="token punctuation">;</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>msg<span class="token punctuation">.</span><span class="token function">ParseFromArray</span><span class="token punctuation">(</span>pPdu<span class="token operator">-&gt;</span><span class="token function">GetBodyData</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> pPdu<span class="token operator">-&gt;</span><span class="token function">GetBodyLength</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token punctuation">{</span>

        string strDomain <span class="token operator">=</span> msg<span class="token punctuation">.</span><span class="token function">user_name</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        string strPass <span class="token operator">=</span> msg<span class="token punctuation">.</span><span class="token function">password</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        msgResp<span class="token punctuation">.</span><span class="token function">set_user_name</span><span class="token punctuation">(</span>strDomain<span class="token punctuation">)</span><span class="token punctuation">;</span>
        msgResp<span class="token punctuation">.</span><span class="token function">set_attach_data</span><span class="token punctuation">(</span>msg<span class="token punctuation">.</span><span class="token function">attach_data</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token keyword">do</span>
        <span class="token punctuation">{</span>
            CAutoLock <span class="token function">cAutoLock</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>g_cLimitLock<span class="token punctuation">)</span><span class="token punctuation">;</span>
            list<span class="token operator">&lt;</span><span class="token keyword">uint32_t</span><span class="token operator">&gt;</span><span class="token operator">&amp;</span> lsErrorTime <span class="token operator">=</span> g_hmLimits<span class="token punctuation">[</span>strDomain<span class="token punctuation">]</span><span class="token punctuation">;</span>
            <span class="token keyword">uint32_t</span> tmNow <span class="token operator">=</span> <span class="token function">time</span><span class="token punctuation">(</span><span class="token constant">NULL</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

            <span class="token comment">//&#x6E05;&#x7406;&#x8D85;&#x8FC7;30&#x5206;&#x949F;&#x7684;&#x9519;&#x8BEF;&#x65F6;&#x95F4;&#x70B9;&#x8BB0;&#x5F55;</span>
            <span class="token comment">/*
             &#x6E05;&#x7406;&#x653E;&#x5728;&#x8FD9;&#x91CC;&#x8FD8;&#x662F;&#x653E;&#x5728;&#x5BC6;&#x7801;&#x9519;&#x8BEF;&#x540E;&#x6DFB;&#x52A0;&#x7684;&#x65F6;&#x5019;&#x5462;&#xFF1F;
             &#x653E;&#x5728;&#x8FD9;&#x91CC;&#xFF0C;&#x6BCF;&#x6B21;&#x90FD;&#x8981;&#x904D;&#x5386;&#xFF0C;&#x4F1A;&#x6709;&#x4E00;&#x70B9;&#x70B9;&#x6027;&#x80FD;&#x7684;&#x635F;&#x5931;&#x3002;
             &#x653E;&#x5728;&#x540E;&#x9762;&#xFF0C;&#x53EF;&#x80FD;&#x4F1A;&#x9020;&#x6210;30&#x5206;&#x949F;&#x4E4B;&#x524D;&#x6709;10&#x6B21;&#x9519;&#x7684;&#xFF0C;&#x4F46;&#x662F;&#x672C;&#x6B21;&#x662F;&#x5BF9;&#x7684;&#x5C31;&#x6CA1;&#x529E;&#x6CD5;&#x518D;&#x8BBF;&#x95EE;&#x4E86;&#x3002;
             */</span>
            <span class="token keyword">auto</span> itTime<span class="token operator">=</span>lsErrorTime<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token punctuation">;</span> itTime<span class="token operator">!=</span>lsErrorTime<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token operator">++</span>itTime<span class="token punctuation">)</span>
            <span class="token punctuation">{</span>
                <span class="token keyword">if</span><span class="token punctuation">(</span>tmNow <span class="token operator">-</span> <span class="token operator">*</span>itTime <span class="token operator">&gt;</span> <span class="token number">30</span><span class="token operator">*</span><span class="token number">60</span><span class="token punctuation">)</span>
                <span class="token punctuation">{</span>
                    <span class="token keyword">break</span><span class="token punctuation">;</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span>
            <span class="token keyword">if</span><span class="token punctuation">(</span>itTime <span class="token operator">!=</span> lsErrorTime<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
            <span class="token punctuation">{</span>
                lsErrorTime<span class="token punctuation">.</span><span class="token function">erase</span><span class="token punctuation">(</span>itTime<span class="token punctuation">,</span> lsErrorTime<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>

            <span class="token comment">// &#x5224;&#x65AD;30&#x5206;&#x949F;&#x5185;&#x5BC6;&#x7801;&#x9519;&#x8BEF;&#x6B21;&#x6570;&#x662F;&#x5426;&#x5927;&#x4E8E;10</span>
            <span class="token keyword">if</span><span class="token punctuation">(</span>lsErrorTime<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">&gt;</span> <span class="token number">10</span><span class="token punctuation">)</span>
            <span class="token punctuation">{</span>
                itTime <span class="token operator">=</span> lsErrorTime<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token keyword">if</span><span class="token punctuation">(</span>tmNow <span class="token operator">-</span> <span class="token operator">*</span>itTime <span class="token operator">&lt;=</span> <span class="token number">30</span><span class="token operator">*</span><span class="token number">60</span><span class="token punctuation">)</span>
                <span class="token punctuation">{</span>
                    msgResp<span class="token punctuation">.</span><span class="token function">set_result_code</span><span class="token punctuation">(</span><span class="token number">6</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    msgResp<span class="token punctuation">.</span><span class="token function">set_result_string</span><span class="token punctuation">(</span><span class="token string">&quot;&#x7528;&#x6237;&#x540D;/&#x5BC6;&#x7801;&#x9519;&#x8BEF;&#x6B21;&#x6570;&#x592A;&#x591A;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    pPduResp<span class="token operator">-&gt;</span><span class="token function">SetPBMsg</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>msgResp<span class="token punctuation">)</span><span class="token punctuation">;</span>
                    pPduResp<span class="token operator">-&gt;</span><span class="token function">SetSeqNum</span><span class="token punctuation">(</span>pPdu<span class="token operator">-&gt;</span><span class="token function">GetSeqNum</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    pPduResp<span class="token operator">-&gt;</span><span class="token function">SetServiceId</span><span class="token punctuation">(</span>IM<span class="token operator">::</span>BaseDefine<span class="token operator">::</span>SID_OTHER<span class="token punctuation">)</span><span class="token punctuation">;</span>
                    pPduResp<span class="token operator">-&gt;</span><span class="token function">SetCommandId</span><span class="token punctuation">(</span>IM<span class="token operator">::</span>BaseDefine<span class="token operator">::</span>CID_OTHER_VALIDATE_RSP<span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token class-name">CProxyConn</span><span class="token operator">::</span><span class="token function">AddResponsePdu</span><span class="token punctuation">(</span>conn_uuid<span class="token punctuation">,</span> pPduResp<span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token keyword">return</span> <span class="token punctuation">;</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span> <span class="token keyword">while</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;%s request login.&quot;</span><span class="token punctuation">,</span> strDomain<span class="token punctuation">.</span><span class="token function">c_str</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>



        IM<span class="token operator">::</span>BaseDefine<span class="token operator">::</span>UserInfo cUser<span class="token punctuation">;</span>

        <span class="token keyword">if</span><span class="token punctuation">(</span>g_loginStrategy<span class="token punctuation">.</span><span class="token function">doLogin</span><span class="token punctuation">(</span>strDomain<span class="token punctuation">,</span> strPass<span class="token punctuation">,</span> cUser<span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token punctuation">{</span>
            IM<span class="token operator">::</span>BaseDefine<span class="token operator">::</span>UserInfo<span class="token operator">*</span> pUser <span class="token operator">=</span> msgResp<span class="token punctuation">.</span><span class="token function">mutable_user_info</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            pUser<span class="token operator">-&gt;</span><span class="token function">set_user_id</span><span class="token punctuation">(</span>cUser<span class="token punctuation">.</span><span class="token function">user_id</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            pUser<span class="token operator">-&gt;</span><span class="token function">set_user_gender</span><span class="token punctuation">(</span>cUser<span class="token punctuation">.</span><span class="token function">user_gender</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            pUser<span class="token operator">-&gt;</span><span class="token function">set_department_id</span><span class="token punctuation">(</span>cUser<span class="token punctuation">.</span><span class="token function">department_id</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            pUser<span class="token operator">-&gt;</span><span class="token function">set_user_nick_name</span><span class="token punctuation">(</span>cUser<span class="token punctuation">.</span><span class="token function">user_nick_name</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            pUser<span class="token operator">-&gt;</span><span class="token function">set_user_domain</span><span class="token punctuation">(</span>cUser<span class="token punctuation">.</span><span class="token function">user_domain</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            pUser<span class="token operator">-&gt;</span><span class="token function">set_avatar_url</span><span class="token punctuation">(</span>cUser<span class="token punctuation">.</span><span class="token function">avatar_url</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

            pUser<span class="token operator">-&gt;</span><span class="token function">set_email</span><span class="token punctuation">(</span>cUser<span class="token punctuation">.</span><span class="token function">email</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            pUser<span class="token operator">-&gt;</span><span class="token function">set_user_tel</span><span class="token punctuation">(</span>cUser<span class="token punctuation">.</span><span class="token function">user_tel</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            pUser<span class="token operator">-&gt;</span><span class="token function">set_user_real_name</span><span class="token punctuation">(</span>cUser<span class="token punctuation">.</span><span class="token function">user_real_name</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            pUser<span class="token operator">-&gt;</span><span class="token function">set_status</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

            pUser<span class="token operator">-&gt;</span><span class="token function">set_sign_info</span><span class="token punctuation">(</span>cUser<span class="token punctuation">.</span><span class="token function">sign_info</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

            msgResp<span class="token punctuation">.</span><span class="token function">set_result_code</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            msgResp<span class="token punctuation">.</span><span class="token function">set_result_string</span><span class="token punctuation">(</span><span class="token string">&quot;&#x6210;&#x529F;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

            <span class="token comment">//&#x5982;&#x679C;&#x767B;&#x9646;&#x6210;&#x529F;&#xFF0C;&#x5219;&#x6E05;&#x9664;&#x9519;&#x8BEF;&#x5C1D;&#x8BD5;&#x9650;&#x5236;</span>
            CAutoLock <span class="token function">cAutoLock</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>g_cLimitLock<span class="token punctuation">)</span><span class="token punctuation">;</span>
            list<span class="token operator">&lt;</span><span class="token keyword">uint32_t</span><span class="token operator">&gt;</span><span class="token operator">&amp;</span> lsErrorTime <span class="token operator">=</span> g_hmLimits<span class="token punctuation">[</span>strDomain<span class="token punctuation">]</span><span class="token punctuation">;</span>
            lsErrorTime<span class="token punctuation">.</span><span class="token function">clear</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">else</span>
        <span class="token punctuation">{</span>
            <span class="token comment">//&#x5BC6;&#x7801;&#x9519;&#x8BEF;&#xFF0C;&#x8BB0;&#x5F55;&#x4E00;&#x6B21;&#x767B;&#x9646;&#x5931;&#x8D25;</span>
            <span class="token keyword">uint32_t</span> tmCurrent <span class="token operator">=</span> <span class="token function">time</span><span class="token punctuation">(</span><span class="token constant">NULL</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            CAutoLock <span class="token function">cAutoLock</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>g_cLimitLock<span class="token punctuation">)</span><span class="token punctuation">;</span>
            list<span class="token operator">&lt;</span><span class="token keyword">uint32_t</span><span class="token operator">&gt;</span><span class="token operator">&amp;</span> lsErrorTime <span class="token operator">=</span> g_hmLimits<span class="token punctuation">[</span>strDomain<span class="token punctuation">]</span><span class="token punctuation">;</span>
            lsErrorTime<span class="token punctuation">.</span><span class="token function">push_front</span><span class="token punctuation">(</span>tmCurrent<span class="token punctuation">)</span><span class="token punctuation">;</span>

            <span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;get result false&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            msgResp<span class="token punctuation">.</span><span class="token function">set_result_code</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            msgResp<span class="token punctuation">.</span><span class="token function">set_result_string</span><span class="token punctuation">(</span><span class="token string">&quot;&#x7528;&#x6237;&#x540D;/&#x5BC6;&#x7801;&#x9519;&#x8BEF;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">else</span>
    <span class="token punctuation">{</span>
        msgResp<span class="token punctuation">.</span><span class="token function">set_result_code</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        msgResp<span class="token punctuation">.</span><span class="token function">set_result_string</span><span class="token punctuation">(</span><span class="token string">&quot;&#x670D;&#x52A1;&#x7AEF;&#x5185;&#x90E8;&#x9519;&#x8BEF;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>


    pPduResp<span class="token operator">-&gt;</span><span class="token function">SetPBMsg</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>msgResp<span class="token punctuation">)</span><span class="token punctuation">;</span>
    pPduResp<span class="token operator">-&gt;</span><span class="token function">SetSeqNum</span><span class="token punctuation">(</span>pPdu<span class="token operator">-&gt;</span><span class="token function">GetSeqNum</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    pPduResp<span class="token operator">-&gt;</span><span class="token function">SetServiceId</span><span class="token punctuation">(</span>IM<span class="token operator">::</span>BaseDefine<span class="token operator">::</span>SID_OTHER<span class="token punctuation">)</span><span class="token punctuation">;</span>
    pPduResp<span class="token operator">-&gt;</span><span class="token function">SetCommandId</span><span class="token punctuation">(</span>IM<span class="token operator">::</span>BaseDefine<span class="token operator">::</span>CID_OTHER_VALIDATE_RSP<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">CProxyConn</span><span class="token operator">::</span><span class="token function">AddResponsePdu</span><span class="token punctuation">(</span>conn_uuid<span class="token punctuation">,</span> pPduResp<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x8FD9;&#x6BB5;&#x4EE3;&#x7801;&#x6709;&#x70B9;&#x590D;&#x6742;&#xFF0C;&#x4E0B;&#x9762;&#x5206;&#x6790;&#x4E4B;&#xFF1A;</p>
<p>&#x9996;&#x5148;&#xFF0C;&#x5C06;&#x767B;&#x5F55;&#x8BF7;&#x6C42;&#x5305;&#x6570;&#x636E;&#x901A;&#x8FC7;&#x51FD;&#x6570;&#x53C2;&#x6570;&#xFF08;&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#xFF09;&#x4F20;&#x5165;&#x8FDB;&#x6765;&#xFF0C;&#x5176;&#x6B21;&#x662F;&#x8FDE;&#x63A5;&#x5BF9;&#x8C61;&#x7684;id&#x3002;&#x524D;&#x9762;&#x5DF2;&#x7ECF;&#x4ECB;&#x7ECD;&#x8FC7;&#x4E86;&#xFF0C;&#x6BCF;&#x4E00;&#x4E2A;&#x65B0;&#x7684;socket&#x4E0D;&#x4EC5;&#x5BF9;&#x5E94;&#x4E00;&#x4E2A;CBaseSocket&#x5BF9;&#x8C61;&#xFF0C;&#x540C;&#x65F6;&#x4E5F;&#x5BF9;&#x5E94;&#x4E00;&#x4E2A;&#x8FDE;&#x63A5;&#x5BF9;&#x8C61;CImConn&#xFF08;&#x53EF;&#x80FD;&#x4F1A;&#x88AB;&#x5177;&#x4F53;&#x5316;&#x6210;&#x5BF9;&#x5E94;&#x7684;&#x5B50;&#x7C7B;&#xFF0C;&#x5982;CProxyConn&#xFF09;&#x3002;&#x8FD9;&#x4E9B;&#x8FDE;&#x63A5;&#x5BF9;&#x8C61;&#x88AB;&#x653E;&#x5728;&#x53E6;&#x5916;&#x4E00;&#x4E2A;&#x5168;&#x5C40;map g_proxy_conn_map&#x91CC;&#x9762;&#x8FDB;&#x884C;&#x7BA1;&#x7406;&#x3002;</p>
<p>&#x901A;&#x8FC7;&#x5305;&#x6570;&#x636E;&#xFF0C;&#x6211;&#x4EEC;&#x80FD;&#x5F97;&#x5230;&#x767B;&#x5F55;&#x7684;&#x7528;&#x6237;&#x540D;&#x548C;&#x5BC6;&#x7801;&#x7B49;&#x4FE1;&#x606F;&#x3002;&#x63A5;&#x7740;&#x68C0;&#x6D4B;30&#x5206;&#x949F;&#x4E4B;&#x5185;&#xFF0C;&#x5C1D;&#x8BD5;&#x767B;&#x5F55;&#x7684;&#x6B21;&#x6570;&#xFF0C;&#x5982;&#x679C;30&#x5206;&#x949F;&#x4E4B;&#x5185;&#x5BC6;&#x7801;&#x9519;&#x8BEF;&#x6B21;&#x6570;&#x8D85;&#x8FC7;10&#x6B64;&#x3002;&#x5219;&#x4E0D;&#x5141;&#x8BB8;&#x767B;&#x5F55;&#x3002;&#x7EC4;&#x6210;&#x4E00;&#x4E2A;&#x63D0;&#x793A;&#x201C;&#x7528;&#x6237;&#x540D;&#x6216;&#x5BC6;&#x7801;&#x9519;&#x8BEF;&#x6B64;&#x65F6;&#x592A;&#x591A;&#x201D;&#x7684;&#x5305;&#xFF1A;</p>
<pre class="language-"><code class="lang-cpp">msgResp<span class="token punctuation">.</span><span class="token function">set_result_code</span><span class="token punctuation">(</span><span class="token number">6</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
msgResp<span class="token punctuation">.</span><span class="token function">set_result_string</span><span class="token punctuation">(</span><span class="token string">&quot;&#x7528;&#x6237;&#x540D;/&#x5BC6;&#x7801;&#x9519;&#x8BEF;&#x6B21;&#x6570;&#x592A;&#x591A;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
pPduResp<span class="token operator">-&gt;</span><span class="token function">SetPBMsg</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>msgResp<span class="token punctuation">)</span><span class="token punctuation">;</span>
pPduResp<span class="token operator">-&gt;</span><span class="token function">SetSeqNum</span><span class="token punctuation">(</span>pPdu<span class="token operator">-&gt;</span><span class="token function">GetSeqNum</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
pPduResp<span class="token operator">-&gt;</span><span class="token function">SetServiceId</span><span class="token punctuation">(</span>IM<span class="token operator">::</span>BaseDefine<span class="token operator">::</span>SID_OTHER<span class="token punctuation">)</span><span class="token punctuation">;</span>
pPduResp<span class="token operator">-&gt;</span><span class="token function">SetCommandId</span><span class="token punctuation">(</span>IM<span class="token operator">::</span>BaseDefine<span class="token operator">::</span>CID_OTHER_VALIDATE_RSP<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">CProxyConn</span><span class="token operator">::</span><span class="token function">AddResponsePdu</span><span class="token punctuation">(</span>conn_uuid<span class="token punctuation">,</span> pPduResp<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
<p>&#x5982;&#x679C;&#x4E0D;&#x5B58;&#x5728;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#xFF0C;&#x5219;&#x63A5;&#x7740;&#x8C03;&#x7528;g_loginStrategy.doLogin(strDomain, strPass, cUser)&#x8FDE;&#x63A5;&#x6570;&#x636E;&#x5E93;&#x8FDB;&#x884C;&#x7528;&#x6237;&#x540D;&#x548C;&#x5BC6;&#x7801;&#x6821;&#x9A8C;&#xFF1A;</p>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">bool</span> <span class="token class-name">CInterLoginStrategy</span><span class="token operator">::</span><span class="token function">doLogin</span><span class="token punctuation">(</span><span class="token keyword">const</span> std<span class="token operator">::</span>string <span class="token operator">&amp;</span>strName<span class="token punctuation">,</span> <span class="token keyword">const</span> std<span class="token operator">::</span>string <span class="token operator">&amp;</span>strPass<span class="token punctuation">,</span> IM<span class="token operator">::</span>BaseDefine<span class="token operator">::</span>UserInfo<span class="token operator">&amp;</span> user<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token keyword">bool</span> bRet <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">;</span>
    CDBManager<span class="token operator">*</span> pDBManger <span class="token operator">=</span> <span class="token class-name">CDBManager</span><span class="token operator">::</span><span class="token function">getInstance</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    CDBConn<span class="token operator">*</span> pDBConn <span class="token operator">=</span> pDBManger<span class="token operator">-&gt;</span><span class="token function">GetDBConn</span><span class="token punctuation">(</span><span class="token string">&quot;teamtalk_slave&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>pDBConn<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        string strSql <span class="token operator">=</span> <span class="token string">&quot;select * from IMUser where name=&apos;&quot;</span> <span class="token operator">+</span> strName <span class="token operator">+</span> <span class="token string">&quot;&apos; and status=0&quot;</span><span class="token punctuation">;</span>
        CResultSet<span class="token operator">*</span> pResultSet <span class="token operator">=</span> pDBConn<span class="token operator">-&gt;</span><span class="token function">ExecuteQuery</span><span class="token punctuation">(</span>strSql<span class="token punctuation">.</span><span class="token function">c_str</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span>pResultSet<span class="token punctuation">)</span>
        <span class="token punctuation">{</span>
            string strResult<span class="token punctuation">,</span> strSalt<span class="token punctuation">;</span>
            <span class="token keyword">uint32_t</span> nId<span class="token punctuation">,</span> nGender<span class="token punctuation">,</span> nDeptId<span class="token punctuation">,</span> nStatus<span class="token punctuation">;</span>
            string strNick<span class="token punctuation">,</span> strAvatar<span class="token punctuation">,</span> strEmail<span class="token punctuation">,</span> strRealName<span class="token punctuation">,</span> strTel<span class="token punctuation">,</span> strDomain<span class="token punctuation">,</span>strSignInfo<span class="token punctuation">;</span>
            <span class="token keyword">while</span> <span class="token punctuation">(</span>pResultSet<span class="token operator">-&gt;</span><span class="token function">Next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                nId <span class="token operator">=</span> pResultSet<span class="token operator">-&gt;</span><span class="token function">GetInt</span><span class="token punctuation">(</span><span class="token string">&quot;id&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                strResult <span class="token operator">=</span> pResultSet<span class="token operator">-&gt;</span><span class="token function">GetString</span><span class="token punctuation">(</span><span class="token string">&quot;password&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                strSalt <span class="token operator">=</span> pResultSet<span class="token operator">-&gt;</span><span class="token function">GetString</span><span class="token punctuation">(</span><span class="token string">&quot;salt&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

                strNick <span class="token operator">=</span> pResultSet<span class="token operator">-&gt;</span><span class="token function">GetString</span><span class="token punctuation">(</span><span class="token string">&quot;nick&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                nGender <span class="token operator">=</span> pResultSet<span class="token operator">-&gt;</span><span class="token function">GetInt</span><span class="token punctuation">(</span><span class="token string">&quot;sex&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                strRealName <span class="token operator">=</span> pResultSet<span class="token operator">-&gt;</span><span class="token function">GetString</span><span class="token punctuation">(</span><span class="token string">&quot;name&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                strDomain <span class="token operator">=</span> pResultSet<span class="token operator">-&gt;</span><span class="token function">GetString</span><span class="token punctuation">(</span><span class="token string">&quot;domain&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                strTel <span class="token operator">=</span> pResultSet<span class="token operator">-&gt;</span><span class="token function">GetString</span><span class="token punctuation">(</span><span class="token string">&quot;phone&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                strEmail <span class="token operator">=</span> pResultSet<span class="token operator">-&gt;</span><span class="token function">GetString</span><span class="token punctuation">(</span><span class="token string">&quot;email&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                strAvatar <span class="token operator">=</span> pResultSet<span class="token operator">-&gt;</span><span class="token function">GetString</span><span class="token punctuation">(</span><span class="token string">&quot;avatar&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                nDeptId <span class="token operator">=</span> pResultSet<span class="token operator">-&gt;</span><span class="token function">GetInt</span><span class="token punctuation">(</span><span class="token string">&quot;departId&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                nStatus <span class="token operator">=</span> pResultSet<span class="token operator">-&gt;</span><span class="token function">GetInt</span><span class="token punctuation">(</span><span class="token string">&quot;status&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                strSignInfo <span class="token operator">=</span> pResultSet<span class="token operator">-&gt;</span><span class="token function">GetString</span><span class="token punctuation">(</span><span class="token string">&quot;sign_info&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

            <span class="token punctuation">}</span>

            string strInPass <span class="token operator">=</span> strPass <span class="token operator">+</span> strSalt<span class="token punctuation">;</span>
            <span class="token keyword">char</span> szMd5<span class="token punctuation">[</span><span class="token number">33</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
            <span class="token class-name">CMd5</span><span class="token operator">::</span><span class="token function">MD5_Calculate</span><span class="token punctuation">(</span>strInPass<span class="token punctuation">.</span><span class="token function">c_str</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> strInPass<span class="token punctuation">.</span><span class="token function">length</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> szMd5<span class="token punctuation">)</span><span class="token punctuation">;</span>
            string <span class="token function">strOutPass</span><span class="token punctuation">(</span>szMd5<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment">//&#x53BB;&#x6389;&#x5BC6;&#x7801;&#x6821;&#x9A8C;</span>
            <span class="token comment">//if(strOutPass == strResult)</span>
            <span class="token punctuation">{</span>
                bRet <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
                user<span class="token punctuation">.</span><span class="token function">set_user_id</span><span class="token punctuation">(</span>nId<span class="token punctuation">)</span><span class="token punctuation">;</span>
                user<span class="token punctuation">.</span><span class="token function">set_user_nick_name</span><span class="token punctuation">(</span>strNick<span class="token punctuation">)</span><span class="token punctuation">;</span>
                user<span class="token punctuation">.</span><span class="token function">set_user_gender</span><span class="token punctuation">(</span>nGender<span class="token punctuation">)</span><span class="token punctuation">;</span>
                user<span class="token punctuation">.</span><span class="token function">set_user_real_name</span><span class="token punctuation">(</span>strRealName<span class="token punctuation">)</span><span class="token punctuation">;</span>
                user<span class="token punctuation">.</span><span class="token function">set_user_domain</span><span class="token punctuation">(</span>strDomain<span class="token punctuation">)</span><span class="token punctuation">;</span>
                user<span class="token punctuation">.</span><span class="token function">set_user_tel</span><span class="token punctuation">(</span>strTel<span class="token punctuation">)</span><span class="token punctuation">;</span>
                user<span class="token punctuation">.</span><span class="token function">set_email</span><span class="token punctuation">(</span>strEmail<span class="token punctuation">)</span><span class="token punctuation">;</span>
                user<span class="token punctuation">.</span><span class="token function">set_avatar_url</span><span class="token punctuation">(</span>strAvatar<span class="token punctuation">)</span><span class="token punctuation">;</span>
                user<span class="token punctuation">.</span><span class="token function">set_department_id</span><span class="token punctuation">(</span>nDeptId<span class="token punctuation">)</span><span class="token punctuation">;</span>
                user<span class="token punctuation">.</span><span class="token function">set_status</span><span class="token punctuation">(</span>nStatus<span class="token punctuation">)</span><span class="token punctuation">;</span>
              user<span class="token punctuation">.</span><span class="token function">set_sign_info</span><span class="token punctuation">(</span>strSignInfo<span class="token punctuation">)</span><span class="token punctuation">;</span>

            <span class="token punctuation">}</span>
            <span class="token keyword">delete</span>  pResultSet<span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        pDBManger<span class="token operator">-&gt;</span><span class="token function">RelDBConn</span><span class="token punctuation">(</span>pDBConn<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">return</span> bRet<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x8FD9;&#x91CC;&#x4E5F;&#x9700;&#x8981;&#x4E00;&#x4E2A;mysql&#x8FDE;&#x63A5;&#xFF0C;&#x8FD9;&#x4E2A;&#x8FDE;&#x63A5;&#x7684;&#x5206;&#x914D;&#x65B9;&#x5F0F;&#x5728;&#x524D;&#x9762;&#x4ECB;&#x7ECD;&#x8FC7;&#x4E86;&#x3002;&#x5373;&#x5728;&#x8FDE;&#x63A5;&#x6C60;&#x4E2D;&#x968F;&#x673A;&#x62FF;&#x4E00;&#x4E2A;&#xFF0C;&#x5982;&#x679C;&#x6C60;&#x4E2D;&#x4E0D;&#x5B58;&#x5728;&#xFF0C;&#x5219;&#x65B0;&#x5EFA;&#x4E00;&#x4E2A;&#x3002;&#x7528;&#x5B8C;&#x8FD8;&#x56DE;&#x53BB;&#xFF1A;</p>
<pre class="language-"><code class="lang-cpp">pDBManger<span class="token operator">-&gt;</span><span class="token function">RelDBConn</span><span class="token punctuation">(</span>pDBConn<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
<p>&#x63A5;&#x7740;&#x901A;&#x8FC7;&#x7528;&#x6237;&#x540D;&#x4ECE;&#x6570;&#x636E;&#x5E93;&#x4E2D;&#x53D6;&#x51FA;&#x8BE5;&#x7528;&#x6237;&#x4FE1;&#x606F;&#xFF0C;&#x5982;&#x679C;&#x8BB0;&#x5F55;&#x5B58;&#x5728;&#xFF0C;&#x5219;&#x63A5;&#x7740;&#x6821;&#x9A8C;&#x5BC6;&#x7801;&#x3002;&#x5BC6;&#x7801;&#x5728;&#x6570;&#x636E;&#x5E93;&#x91CC;&#x9762;&#x7684;&#x5B58;&#x50A8;&#x5F62;&#x5F0F;&#x662F;&#xFF1A;&#x5BC6;&#x7801;+&#x7528;&#x6237;&#x7684;salt&#x503C; &#x7EC4;&#x6210;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#x7684;md5&#x503C;&#x3002;&#x5BC6;&#x7801;&#x5982;&#x679C;&#x4E5F;&#x6821;&#x9A8C;&#x6B63;&#x786E;&#xFF0C;&#x7EC4;&#x88C5;&#x6210;&#x4E00;&#x4E2A;&#x6B63;&#x786E;&#x5E94;&#x7B54;&#x6570;&#x636E;&#x5305;&#xFF08;&#x9644;&#x4E0A;&#x547D;&#x4EE4;&#x53F7;&#x3001;&#x5E8F;&#x5217;&#x53F7;&#x3001;&#x63D0;&#x793A;&#x4FE1;&#x606F;&#x7B49;&#xFF09;&#xFF1A;</p>
<pre class="language-"><code class="lang-cpp">pPduResp<span class="token operator">-&gt;</span><span class="token function">SetPBMsg</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>msgResp<span class="token punctuation">)</span><span class="token punctuation">;</span>
    pPduResp<span class="token operator">-&gt;</span><span class="token function">SetSeqNum</span><span class="token punctuation">(</span>pPdu<span class="token operator">-&gt;</span><span class="token function">GetSeqNum</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    pPduResp<span class="token operator">-&gt;</span><span class="token function">SetServiceId</span><span class="token punctuation">(</span>IM<span class="token operator">::</span>BaseDefine<span class="token operator">::</span>SID_OTHER<span class="token punctuation">)</span><span class="token punctuation">;</span>
    pPduResp<span class="token operator">-&gt;</span><span class="token function">SetCommandId</span><span class="token punctuation">(</span>IM<span class="token operator">::</span>BaseDefine<span class="token operator">::</span>CID_OTHER_VALIDATE_RSP<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">CProxyConn</span><span class="token operator">::</span><span class="token function">AddResponsePdu</span><span class="token punctuation">(</span>conn_uuid<span class="token punctuation">,</span> pPduResp<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
<p>&#x73B0;&#x5728;&#x4E0D;&#x7BA1;&#x767B;&#x5F55;&#x6210;&#x529F;&#x4E0E;&#x5426;&#xFF0C;&#x767B;&#x5F55;&#x5E94;&#x7B54;&#x5305;&#x4E5F;&#x5DF2;&#x7ECF;&#x7EC4;&#x88C5;&#x597D;&#x4E86;&#x3002;&#x63A5;&#x4E0B;&#x6765;&#xFF0C;&#x5C31;&#x662F;&#x5982;&#x4F55;&#x53D1;&#x51FA;&#x53BB;&#x4E86;&#xFF1F;&#x4E0A;&#x8FF0;&#x4EE3;&#x7801;&#x6700;&#x540E;&#x4E00;&#x884C;&#xFF1A;</p>
<pre class="language-"><code>CProxyConn::AddResponsePdu(conn_uuid, pPduResp)
</code></pre><p>&#x5176;&#x8C03;&#x7528;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">void</span> <span class="token class-name">CProxyConn</span><span class="token operator">::</span><span class="token function">AddResponsePdu</span><span class="token punctuation">(</span><span class="token keyword">uint32_t</span> conn_uuid<span class="token punctuation">,</span> CImPdu<span class="token operator">*</span> pPdu<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    ResponsePdu_t<span class="token operator">*</span> pResp <span class="token operator">=</span> <span class="token keyword">new</span> ResponsePdu_t<span class="token punctuation">;</span>
    pResp<span class="token operator">-&gt;</span>conn_uuid <span class="token operator">=</span> conn_uuid<span class="token punctuation">;</span>
    pResp<span class="token operator">-&gt;</span>pPdu <span class="token operator">=</span> pPdu<span class="token punctuation">;</span>

    s_list_lock<span class="token punctuation">.</span><span class="token function">lock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    s_response_pdu_list<span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span>pResp<span class="token punctuation">)</span><span class="token punctuation">;</span>
    s_list_lock<span class="token punctuation">.</span><span class="token function">unlock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x6211;&#x4EEC;&#x8FD9;&#x91CC;&#x5E76;&#x6CA1;&#x6709;&#x76F4;&#x63A5;&#x5C06;&#x5E94;&#x7B54;&#x6570;&#x636E;&#x5305;&#x901A;&#x8FC7;&#x8FDE;&#x63A5;&#x5BF9;&#x8C61;CProxyConn&#x53D1;&#x51FA;&#x53BB;&#x3002;&#x56E0;&#x4E3A;&#x76F4;&#x63A5;&#x53D1;&#x51FA;&#x53BB;&#xFF0C;&#x672A;&#x5FC5;&#x80FD;&#x53D1;&#x51FA;&#x53BB;&#x3002;&#x8FD9;&#x4F1A;&#x5BFC;&#x81F4;&#x7A0B;&#x5E8F;&#x963B;&#x585E;&#x3002;&#xFF08;&#x539F;&#x56E0;&#x662F;&#xFF1A;&#x5BF9;&#x65B9;&#x7684;tcp&#x7A97;&#x53E3;&#x592A;&#x5C0F;&#xFF0C;&#x5BFC;&#x81F4;tcp&#x7A97;&#x53E3;&#x592A;&#x5C0F;&#x7684;&#x5E38;&#x89C1;&#x539F;&#x56E0;&#x662F;&#xFF1A;&#x5BF9;&#x65B9;&#x65E0;&#x6CD5;&#x6536;&#x5305;&#x6216;&#x4E0D;&#x53CA;&#x65F6;&#x6536;&#x5305;&#xFF0C;&#x6570;&#x636E;&#x79EF;&#x538B;&#x5728;&#x5BF9;&#x65B9;&#x7F51;&#x7EDC;&#x534F;&#x8BAE;&#x6808;&#x91CC;&#x9762;&#xFF09;&#x3002;&#x6211;&#x4EEC;&#x8FD9;&#x91CC;&#x662F;&#x5C06;&#x5E94;&#x7B54;&#x6570;&#x636E;&#x5305;&#x653E;&#x5165;&#x8FDE;&#x63A5;&#x5BF9;&#x8C61;&#x7684;&#x4E00;&#x4E2A;&#x5E94;&#x7B54;&#x94FE;&#x8868;s_response_pdu_list&#x4E2D;&#x3002;&#x8FD9;&#x662F;&#x4E00;&#x4E2A;stl list&#x5BB9;&#x5668;&#xFF1A;</p>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">static</span> list<span class="token operator">&lt;</span>ResponsePdu_t<span class="token operator">*</span><span class="token operator">&gt;</span>    s_response_pdu_list<span class="token punctuation">;</span>    <span class="token comment">// &#x4E3B;&#x7EBF;&#x7A0B;&#x53D1;&#x9001;&#x56DE;&#x590D;&#x6D88;&#x606F;</span>
</code></pre>
<p>&#x90A3;&#x4E48;&#xFF0C;&#x5305;&#x5728;&#x8FD9;&#x4E2A;&#x94FE;&#x8868;&#x4E2D;&#xFF0C;&#x4F55;&#x65F6;&#x88AB;&#x53D1;&#x51FA;&#x53BB;&#x5462;&#xFF1F;&#x6211;&#x4EEC;&#x5728;&#x4ECB;&#x7ECD;&#x8BE5;&#x670D;&#x52A1;&#x7684;&#x6D88;&#x606F;&#x6CF5;&#x65F6;&#x4ECB;&#x7ECD;&#x5230;&#x5982;&#x4E0B;&#x6D41;&#x7A0B;&#xFF1A;</p>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">while</span><span class="token punctuation">(</span>&#x9000;&#x51FA;&#x6761;&#x4EF6;<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token comment">//1. &#x904D;&#x5386;&#x5B9A;&#x65F6;&#x5668;&#x961F;&#x5217;&#xFF0C;&#x68C0;&#x6D4B;&#x662F;&#x5426;&#x6709;&#x5B9A;&#x65F6;&#x5668;&#x4E8B;&#x4EF6;&#x5230;&#x671F;&#xFF0C;&#x6709;&#x5219;&#x6267;&#x884C;&#x5B9A;&#x65F6;&#x5668;&#x7684;&#x56DE;&#x8C03;&#x51FD;&#x6570;</span>

    <span class="token comment">//2. &#x904D;&#x5386;&#x5176;&#x4ED6;&#x4EFB;&#x52A1;&#x961F;&#x5217;&#xFF0C;&#x68C0;&#x6D4B;&#x662F;&#x5426;&#x6709;&#x5176;&#x4ED6;&#x4EFB;&#x52A1;&#x9700;&#x8981;&#x6267;&#x884C;&#xFF0C;&#x6709;&#xFF0C;&#x6267;&#x884C;&#x4E4B;</span>

    <span class="token comment">//3. &#x68C0;&#x6D4B;socket&#x96C6;&#x5408;&#xFF0C;&#x5206;&#x79BB;&#x53EF;&#x8BFB;&#x3001;&#x53EF;&#x5199;&#x548C;&#x5F02;&#x5E38;&#x4E8B;&#x4EF6;</span>

    <span class="token comment">//4. &#x5904;&#x7406;socket&#x53EF;&#x8BFB;&#x4E8B;&#x4EF6;</span>

    <span class="token comment">//5. &#x5904;&#x7406;socket&#x53EF;&#x5199;&#x4E8B;&#x4EF6;</span>

    <span class="token comment">//6. &#x5904;&#x7406;socket&#x5F02;&#x5E38;&#x4E8B;&#x4EF6;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x6CE8;&#x610F;&#x7B2C;2&#x6B65;&#xFF1A;&#x904D;&#x5386;&#x5176;&#x4ED6;&#x4EFB;&#x52A1;&#x961F;&#x5217;&#xFF0C;&#x68C0;&#x6D4B;&#x662F;&#x5426;&#x6709;&#x5176;&#x4ED6;&#x4EFB;&#x52A1;&#x9700;&#x8981;&#x6267;&#x884C;&#xFF0C;&#x6709;&#xFF0C;&#x6267;&#x884C;&#x4E4B;&#x3002;&#x6211;&#x4EEC;&#x6765;&#x770B;&#x770B;&#x8FD9;&#x6B65;&#x5177;&#x4F53;&#x505A;&#x4E86;&#x4EC0;&#x4E48;&#x3002;</p>
<p>&#x5728;main&#x51FD;&#x6570;&#x91CC;&#x9762;&#x521D;&#x59CB;&#x5316;&#x4EFB;&#x52A1;&#x961F;&#x5217;&#x7EBF;&#x7A0B;&#x65F6;&#xFF0C;&#x540C;&#x65F6;&#x4E5F;&#x521B;&#x5EFA;&#x4E86;&#x4E00;&#x4E2A;&#x5176;&#x4ED6;&#x4EFB;&#x52A1;&#xFF1A;</p>
<pre class="language-"><code class="lang-cpp"><span class="token function">init_proxy_conn</span><span class="token punctuation">(</span>thread_num<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">int</span> <span class="token function">init_proxy_conn</span><span class="token punctuation">(</span><span class="token keyword">uint32_t</span> thread_num<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    s_handler_map <span class="token operator">=</span> <span class="token class-name">CHandlerMap</span><span class="token operator">::</span><span class="token function">getInstance</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    g_thread_pool<span class="token punctuation">.</span><span class="token function">Init</span><span class="token punctuation">(</span>thread_num<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token function">netlib_add_loop</span><span class="token punctuation">(</span>proxy_loop_callback<span class="token punctuation">,</span> <span class="token constant">NULL</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token function">signal</span><span class="token punctuation">(</span>SIGTERM<span class="token punctuation">,</span> sig_handler<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">return</span> <span class="token function">netlib_register_timer</span><span class="token punctuation">(</span>proxy_timer_callback<span class="token punctuation">,</span> <span class="token constant">NULL</span><span class="token punctuation">,</span> <span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x6CE8;&#x610F;&#x4EE3;&#x7801;netlib_add_loop(proxy_loop_callback, NULL);&#x8BE5;&#x884C;&#x52A0;&#x5165;&#x4E86;&#x4E00;&#x4E2A;&#x5176;&#x4ED6;&#x4EFB;&#x52A1;&#x5230;&#x5176;&#x4ED6;&#x4EFB;&#x52A1;&#x961F;&#x5217;&#x3002;&#x8FD9;&#x6837;&#x5728;&#x4E3B;&#x7EBF;&#x7A0B;&#x7684;&#x6D88;&#x606F;&#x6CF5;&#x4E2D;&#xFF1A;2. &#x904D;&#x5386;&#x5176;&#x4ED6;&#x4EFB;&#x52A1;&#x961F;&#x5217;&#xFF0C;&#x68C0;&#x6D4B;&#x662F;&#x5426;&#x6709;&#x5176;&#x4ED6;&#x4EFB;&#x52A1;&#x9700;&#x8981;&#x6267;&#x884C;&#xFF0C;&#x6709;&#xFF0C;&#x6267;&#x884C;&#x4E4B;&#x3002;</p>
<pre class="language-"><code class="lang-cpp"><span class="token function">_CheckLoop</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">void</span> <span class="token class-name">CEventDispatch</span><span class="token operator">::</span><span class="token function">_CheckLoop</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span>list<span class="token operator">&lt;</span>TimerItem<span class="token operator">*</span><span class="token operator">&gt;</span><span class="token operator">::</span>iterator it <span class="token operator">=</span> m_loop_list<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> it <span class="token operator">!=</span> m_loop_list<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> it<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        TimerItem<span class="token operator">*</span> pItem <span class="token operator">=</span> <span class="token operator">*</span>it<span class="token punctuation">;</span>
        pItem<span class="token operator">-&gt;</span><span class="token function">callback</span><span class="token punctuation">(</span>pItem<span class="token operator">-&gt;</span>user_data<span class="token punctuation">,</span> NETLIB_MSG_LOOP<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token constant">NULL</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x5176;&#x4ED6;&#x4EFB;&#x52A1;&#x7684;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x76EE;&#x524D;&#x53EA;&#x6709;&#x4E00;&#x4E2A;&#xFF0C;&#x5C31;&#x662F;&#x4E0A;&#x9762;&#x8BBE;&#x7F6E;&#x7684;proxy_loop_callback&#xFF1A;</p>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">void</span> <span class="token function">proxy_loop_callback</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token operator">*</span> callback_data<span class="token punctuation">,</span> <span class="token keyword">uint8_t</span> msg<span class="token punctuation">,</span> <span class="token keyword">uint32_t</span> handle<span class="token punctuation">,</span> <span class="token keyword">void</span><span class="token operator">*</span> pParam<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token class-name">CProxyConn</span><span class="token operator">::</span><span class="token function">SendResponsePduList</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">void</span> <span class="token class-name">CProxyConn</span><span class="token operator">::</span><span class="token function">SendResponsePduList</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    s_list_lock<span class="token punctuation">.</span><span class="token function">lock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token operator">!</span>s_response_pdu_list<span class="token punctuation">.</span><span class="token function">empty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        ResponsePdu_t<span class="token operator">*</span> pResp <span class="token operator">=</span> s_response_pdu_list<span class="token punctuation">.</span><span class="token function">front</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        s_response_pdu_list<span class="token punctuation">.</span><span class="token function">pop_front</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        s_list_lock<span class="token punctuation">.</span><span class="token function">unlock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        CProxyConn<span class="token operator">*</span> pConn <span class="token operator">=</span> <span class="token function">get_proxy_conn_by_uuid</span><span class="token punctuation">(</span>pResp<span class="token operator">-&gt;</span>conn_uuid<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>pConn<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>pResp<span class="token operator">-&gt;</span>pPdu<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                pConn<span class="token operator">-&gt;</span><span class="token function">SendPdu</span><span class="token punctuation">(</span>pResp<span class="token operator">-&gt;</span>pPdu<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
                <span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;close connection uuid=%d by parse pdu error\b&quot;</span><span class="token punctuation">,</span> pResp<span class="token operator">-&gt;</span>conn_uuid<span class="token punctuation">)</span><span class="token punctuation">;</span>
                pConn<span class="token operator">-&gt;</span><span class="token function">Close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>

        <span class="token keyword">if</span> <span class="token punctuation">(</span>pResp<span class="token operator">-&gt;</span>pPdu<span class="token punctuation">)</span>
            <span class="token keyword">delete</span> pResp<span class="token operator">-&gt;</span>pPdu<span class="token punctuation">;</span>
        <span class="token keyword">delete</span> pResp<span class="token punctuation">;</span>

        s_list_lock<span class="token punctuation">.</span><span class="token function">lock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    s_list_lock<span class="token punctuation">.</span><span class="token function">unlock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x770B;&#x5230;&#x8FD9;&#x91CC;&#xFF0C;&#x4F60;&#x5E94;&#x8BE5;&#x660E;&#x767D;&#x4E86;&#x3002;&#x539F;&#x6765;&#x5E94;&#x7B54;&#x6570;&#x636E;&#x5305;&#x5728;&#x8FD9;&#x91CC;&#x4ECE;list&#x4E2D;&#x53D6;&#x51FA;&#x6765;&#x3002;&#x7136;&#x540E;&#x8C03;&#x7528;pConn-&gt;SendPdu(pResp-&gt;pPdu)&#x201C;&#x53D1;&#x51FA;&#x53BB;&#x201D;&#x3002;&#x8FD9;&#x91CC;&#x9700;&#x8981;&#x89E3;&#x91CA;&#x4E24;&#x4E2A;&#x95EE;&#x9898;&#xFF1A;&#x7B2C;&#x4E00;&#x4E2A;&#x5C31;&#x662F;&#x4E00;&#x822C;&#x670D;&#x52A1;&#x5668;&#x7AEF;&#x4F1A;&#x6709;&#x591A;&#x4E2A;&#x8FDE;&#x63A5;&#x5BF9;&#x8C61;&#xFF0C;&#x90A3;&#x4E48;&#x5982;&#x4F55;&#x5B9A;&#x4F4D;&#x67D0;&#x4E2A;&#x5E94;&#x7B54;&#x6570;&#x636E;&#x5305;&#x5BF9;&#x5E94;&#x7684;&#x8FDE;&#x63A5;&#x5BF9;&#x8C61;&#x5462;&#xFF1F;&#x8FD9;&#x91CC;&#x5C31;&#x901A;&#x8FC7;&#x6570;&#x636E;&#x5305;&#x672C;&#x8EAB;&#x7684;conn_uuid&#x6765;&#x786E;&#x5B9A;&#xFF1A;</p>
<pre class="language-"><code class="lang-cpp">CProxyConn<span class="token operator">*</span> pConn <span class="token operator">=</span> <span class="token function">get_proxy_conn_by_uuid</span><span class="token punctuation">(</span>pResp<span class="token operator">-&gt;</span>conn_uuid<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
<pre class="language-"><code class="lang-cpp">CProxyConn<span class="token operator">*</span> <span class="token function">get_proxy_conn_by_uuid</span><span class="token punctuation">(</span><span class="token keyword">uint32_t</span> uuid<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    CProxyConn<span class="token operator">*</span> pConn <span class="token operator">=</span> <span class="token constant">NULL</span><span class="token punctuation">;</span>
    UserMap_t<span class="token operator">::</span>iterator it <span class="token operator">=</span> g_uuid_conn_map<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span>uuid<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>it <span class="token operator">!=</span> g_uuid_conn_map<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        pConn <span class="token operator">=</span> <span class="token punctuation">(</span>CProxyConn <span class="token operator">*</span><span class="token punctuation">)</span>it<span class="token operator">-&gt;</span>second<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">return</span> pConn<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x5168;&#x5C40;&#x5BF9;&#x8C61;g_uuid_conn_map&#x91CC;&#x9762;&#x5B58;&#x7684;&#x662F;uuid&#x4E0E;&#x8FDE;&#x63A5;&#x5BF9;&#x8C61;&#x7684;&#x5BF9;&#x5E94;&#x5173;&#x7CFB;&#x3002;&#x8FD9;&#x4E2A;&#x5173;&#x7CFB;&#x4F55;&#x65F6;&#x5B58;&#x5165;&#x5230;&#x8FD9;&#x4E2A;&#x5168;&#x5C40;g_uuid_conn_map&#x5BF9;&#x8C61;&#x7684;&#x5462;&#xFF1F;&#x5728;CProxyConn&#x7684;&#x6784;&#x9020;&#x51FD;&#x6570;&#x4E2D;&#xFF1A;</p>
<pre class="language-"><code class="lang-cpp"><span class="token class-name">CProxyConn</span><span class="token operator">::</span><span class="token function">CProxyConn</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    m_uuid <span class="token operator">=</span> <span class="token operator">++</span>CProxyConn<span class="token operator">::</span>s_uuid_alloctor<span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>m_uuid <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        m_uuid <span class="token operator">=</span> <span class="token operator">++</span>CProxyConn<span class="token operator">::</span>s_uuid_alloctor<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    g_uuid_conn_map<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span>m_uuid<span class="token punctuation">,</span> <span class="token keyword">this</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x8FD9;&#x4E2A;uuid&#x7684;&#x57FA;&#x6570;&#x662F;&#x4E00;&#x4E2A;CProxyConn&#x7684;&#x9759;&#x6001;&#x53D8;&#x91CF;&#xFF1A;</p>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">static</span> <span class="token keyword">uint32_t</span>    s_uuid_alloctor<span class="token punctuation">;</span>
</code></pre>
<p>&#x9ED8;&#x8BA4;&#x662F;0&#xFF1A;</p>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">uint32_t</span> CProxyConn<span class="token operator">::</span>s_uuid_alloctor <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
</code></pre>
<p>&#x4EE5;&#x540E;&#x6BCF;&#x4EA7;&#x751F;&#x4E00;&#x4E2A;&#x65B0;&#x8FDE;&#x63A5;&#x5BF9;&#x8C61;CProxyConn&#xFF0C;&#x5728;&#x6B64;&#x57FA;&#x7840;&#x4E0A;&#x9012;&#x589E;&#xFF0C;&#x56E0;&#x4E3A;&#x6CA1;&#x6709;&#x7528;&#x9501;&#x4FDD;&#x62A4;&#xFF0C;&#x6240;&#x4EE5;&#x53EA;&#x80FD;&#x5728;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x91CC;&#x9762;&#x8C03;&#x7528;&#x3002;&#x800C;CProxyConn&#x6B63;&#x597D;&#x5C31;&#x662F;&#x5728;&#x4E3B;&#x7EBF;&#x7A0B;&#x91CC;&#x9762;&#x4EA7;&#x751F;&#x7684;&#xFF0C;&#x524D;&#x9762;&#x4ECB;&#x7ECD;&#x8FC7;&#x4E86;&#xFF0C;&#x518D;&#x6B21;&#x8D34;&#x4E00;&#x4E0B;&#x4EE3;&#x7801;&#x5427;&#xFF1A;</p>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">void</span> <span class="token function">proxy_serv_callback</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token operator">*</span> callback_data<span class="token punctuation">,</span> <span class="token keyword">uint8_t</span> msg<span class="token punctuation">,</span> <span class="token keyword">uint32_t</span> handle<span class="token punctuation">,</span> <span class="token keyword">void</span><span class="token operator">*</span> pParam<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>msg <span class="token operator">==</span> NETLIB_MSG_CONNECT<span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
        CProxyConn<span class="token operator">*</span> pConn <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token function">CProxyConn</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        pConn<span class="token operator">-&gt;</span><span class="token function">OnConnect</span><span class="token punctuation">(</span>handle<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">else</span>
    <span class="token punctuation">{</span>
        <span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;!!!error msg: %d&quot;</span><span class="token punctuation">,</span> msg<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x8FD9;&#x6837;uuid&#x548C;&#x8FDE;&#x63A5;&#x5BF9;&#x8C61;CProxyConn&#x8FD8;&#x6709;CBaseSocket&#x8FD9;&#x4E09;&#x8005;&#x7684;&#x5173;&#x7CFB;&#x5C31;&#x552F;&#x4E00;&#x7ED1;&#x5B9A;&#x4E86;&#x3002;</p>
<p>&#x63A5;&#x7740;&#x8BF4;&#xFF0C;&#x901A;&#x8FC7;uuid&#x83B7;&#x5F97;&#x5BF9;&#x5E94;&#x6570;&#x636E;&#x5305;&#x7684;&#x8FDE;&#x63A5;&#x5BF9;&#x8C61;&#x540E;&#xFF0C;&#x8C03;&#x7528;&#x5176;&#x65B9;&#x6CD5;pConn-&gt;SendPdu(pResp-&gt;pPdu); &#x201C;&#x53D1;&#x51FA;&#x53BB;&#x201D;&#xFF1F;&#x4F46;&#x662F;&#xFF0C;&#x8FD8;&#x662F;&#x4E0D;&#x884C;&#xFF0C;&#x56E0;&#x4E3A;&#x8FD9;&#x8FD8;&#x6CA1;&#x6709;&#x89E3;&#x51B3;&#x4E0A;&#x6587;&#x63D0;&#x51FA;&#x7684;&#x8BE5;&#x8FDE;&#x63A5;&#x4E0A;&#x5BF9;&#x7AEF;&#x7684;tcp&#x7A97;&#x53E3;&#x592A;&#x5C0F;&#x5BFC;&#x81F4;&#x6570;&#x636E;&#x53D1;&#x4E0D;&#x51FA;&#x7684;&#x95EE;&#x9898;&#x3002;&#x6240;&#x4EE5;pConn-&gt;SendPdu()&#x65B9;&#x6CD5;&#x4E2D;&#x4E00;&#x5B9A;&#x4E0D;&#x662F;&#x8C03;&#x7528;send&#x51FD;&#x6570;&#x76F4;&#x63A5;&#x53D1;&#x9001;&#x6570;&#x636E;&#xFF1A;</p>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">int</span> <span class="token function">SendPdu</span><span class="token punctuation">(</span>CImPdu<span class="token operator">*</span> pPdu<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token function">Send</span><span class="token punctuation">(</span>pPdu<span class="token operator">-&gt;</span><span class="token function">GetBuffer</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> pPdu<span class="token operator">-&gt;</span><span class="token function">GetLength</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>
</code></pre>
<p>&#x5B9E;&#x9645;&#x4E0A;&#x662F;&#x8C03;&#x7528;&#x5176;&#x57FA;&#x7C7B;CImConn&#x7C7B;&#x7684;Send&#x65B9;&#x6CD5;&#xFF0C;&#x53D1;&#x9001;&#x6570;&#x636E;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x5148;&#x8BB0;&#x5F55;&#x4E00;&#x4E0B;&#x53D1;&#x9001;&#x6570;&#x636E;&#x7684;&#x65F6;&#x95F4;&#xFF1A;m_last_send_tick = get_tick_count();</p>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">int</span> <span class="token class-name">CImConn</span><span class="token operator">::</span><span class="token function">Send</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token operator">*</span> data<span class="token punctuation">,</span> <span class="token keyword">int</span> len<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    m_last_send_tick <span class="token operator">=</span> <span class="token function">get_tick_count</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">//    ++g_send_pkt_cnt;</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span>m_busy<span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
        m_out_buf<span class="token punctuation">.</span><span class="token function">Write</span><span class="token punctuation">(</span>data<span class="token punctuation">,</span> len<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> len<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">int</span> offset <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
    <span class="token keyword">int</span> remain <span class="token operator">=</span> len<span class="token punctuation">;</span>
    <span class="token keyword">while</span> <span class="token punctuation">(</span>remain <span class="token operator">&gt;</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">int</span> send_size <span class="token operator">=</span> remain<span class="token punctuation">;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>send_size <span class="token operator">&gt;</span> NETLIB_MAX_SOCKET_BUF_SIZE<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            send_size <span class="token operator">=</span> NETLIB_MAX_SOCKET_BUF_SIZE<span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

        <span class="token keyword">int</span> ret <span class="token operator">=</span> <span class="token function">netlib_send</span><span class="token punctuation">(</span>m_handle<span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token keyword">char</span><span class="token operator">*</span><span class="token punctuation">)</span>data <span class="token operator">+</span> offset<span class="token punctuation">,</span> send_size<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>ret <span class="token operator">&lt;=</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            ret <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
            <span class="token keyword">break</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

        offset <span class="token operator">+=</span> ret<span class="token punctuation">;</span>
        remain <span class="token operator">-=</span> ret<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span>remain <span class="token operator">&gt;</span> <span class="token number">0</span><span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
        m_out_buf<span class="token punctuation">.</span><span class="token function">Write</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token keyword">char</span><span class="token operator">*</span><span class="token punctuation">)</span>data <span class="token operator">+</span> offset<span class="token punctuation">,</span> remain<span class="token punctuation">)</span><span class="token punctuation">;</span>
        m_busy <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
        <span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;send busy, remain=%d &quot;</span><span class="token punctuation">,</span> m_out_buf<span class="token punctuation">.</span><span class="token function">GetWriteOffset</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">else</span>
    <span class="token punctuation">{</span>
        <span class="token function">OnWriteCompelete</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">return</span> len<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x6CE8;&#x610F;&#x8FD9;&#x6BB5;&#x4EE3;&#x7801;&#xFF0C;&#x4E5F;&#x662F;&#x7279;&#x522B;&#x7684;&#x8BB2;&#x7A76;&#xFF1A;</p>
<p>&#x5148;&#x8BD5;&#x7740;&#x8C03;&#x7528;&#x5E95;&#x5C42;send&#x65B9;&#x6CD5;&#x53BB;&#x53D1;&#x9001;&#xFF0C;&#x80FD;&#x53D1;&#x591A;&#x5C11;&#x662F;&#x591A;&#x5C11;&#xFF0C;&#x5269;&#x4E0B;&#x53D1;&#x4E0D;&#x5B8C;&#x7684;&#xFF0C;&#x5199;&#x5165;&#x8BE5;&#x8FDE;&#x63A5;&#x7684;&#x53D1;&#x9001;&#x7F13;&#x51B2;&#x533A;&#x4E2D;&#xFF0C;&#x5E76;&#x5C06;&#x5FD9;&#x788C;&#x6807;&#x5FD7;m_busy&#x7F6E;&#x4F4D;&#xFF08;&#x8BBE;&#x7F6E;&#x4E3A;ture&#xFF09;&#x3002;&#x53CD;&#x4E4B;&#xFF0C;&#x5982;&#x679C;&#x6570;&#x636E;&#x4E00;&#x6B21;&#x6027;&#x53D1;&#x9001;&#x5B8C;&#x6210;&#xFF0C;&#x5219;&#x8C03;&#x7528;&#x6570;&#x636E;&#x53D1;&#x9001;&#x5B8C;&#x6210;&#x51FD;&#x6570;OnWriteComplete&#xFF08;&#xFF09;&#xFF0C;&#x8FD9;&#x4E2A;&#x51FD;&#x6570;&#x76EE;&#x524D;&#x4E3A;&#x7A7A;&#xFF0C;&#x5373;&#x4E0D;&#x505A;&#x4EFB;&#x4F55;&#x4E8B;&#x60C5;&#x3002;</p>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">int</span> ret <span class="token operator">=</span> <span class="token function">netlib_send</span><span class="token punctuation">(</span>m_handle<span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token keyword">char</span><span class="token operator">*</span><span class="token punctuation">)</span>data <span class="token operator">+</span> offset <span class="token punctuation">,</span> send_size<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">int</span> <span class="token function">netlib_send</span><span class="token punctuation">(</span>net_handle_t handle<span class="token punctuation">,</span> <span class="token keyword">void</span><span class="token operator">*</span> buf<span class="token punctuation">,</span> <span class="token keyword">int</span> len<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    CBaseSocket<span class="token operator">*</span> pSocket <span class="token operator">=</span> <span class="token function">FindBaseSocket</span><span class="token punctuation">(</span>handle<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>pSocket<span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
        <span class="token keyword">return</span> NETLIB_ERROR<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">int</span> ret <span class="token operator">=</span> pSocket<span class="token operator">-&gt;</span><span class="token function">Send</span><span class="token punctuation">(</span>buf<span class="token punctuation">,</span> len<span class="token punctuation">)</span><span class="token punctuation">;</span>
    pSocket<span class="token operator">-&gt;</span><span class="token function">ReleaseRef</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> ret<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x4E0A;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x901A;&#x8FC7;socket&#x53E5;&#x67C4;&#x627E;&#x5230;&#x5177;&#x4F53;&#x7684;CBaseSocket&#x5BF9;&#x8C61;&#x3002;&#x63A5;&#x7740;&#x8C03;&#x7528;CBaseSocket::Send()&#x65B9;&#x6CD5;&#xFF1A;</p>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">int</span> <span class="token class-name">CBaseSocket</span><span class="token operator">::</span><span class="token function">Send</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token operator">*</span> buf<span class="token punctuation">,</span> <span class="token keyword">int</span> len<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>m_state <span class="token operator">!=</span> SOCKET_STATE_CONNECTED<span class="token punctuation">)</span>
        <span class="token keyword">return</span> NETLIB_ERROR<span class="token punctuation">;</span>

    <span class="token keyword">int</span> ret <span class="token operator">=</span> <span class="token function">send</span><span class="token punctuation">(</span>m_socket<span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token keyword">char</span><span class="token operator">*</span><span class="token punctuation">)</span>buf<span class="token punctuation">,</span> len<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>ret <span class="token operator">==</span> SOCKET_ERROR<span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
        <span class="token keyword">int</span> err_code <span class="token operator">=</span> <span class="token function">_GetErrorCode</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">_IsBlock</span><span class="token punctuation">(</span>err_code<span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token punctuation">{</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">if</span> <span class="token expression"><span class="token punctuation">(</span><span class="token punctuation">(</span>defined _WIN32<span class="token punctuation">)</span> <span class="token operator">||</span> <span class="token punctuation">(</span>defined __APPLE__<span class="token punctuation">)</span><span class="token punctuation">)</span></span></span>
            <span class="token class-name">CEventDispatch</span><span class="token operator">::</span><span class="token function">Instance</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-&gt;</span><span class="token function">AddEvent</span><span class="token punctuation">(</span>m_socket<span class="token punctuation">,</span> SOCKET_WRITE<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">endif</span></span>
            ret <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
            <span class="token comment">//log(&quot;socket send block fd=%d&quot;, m_socket);</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">else</span>
        <span class="token punctuation">{</span>
            <span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;!!!send failed, error code: %d&quot;</span><span class="token punctuation">,</span> err_code<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">return</span> ret<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x8BE5;&#x65B9;&#x6CD5;&#x53D1;&#x9001;&#x6307;&#x5B9A;&#x957F;&#x5EA6;&#x7684;&#x6570;&#x636E;&#xFF0C;&#x56E0;&#x4E3A;socket&#x5728;&#x521B;&#x5EFA;&#x7684;&#x65F6;&#x5019;&#x88AB;&#x8BBE;&#x7F6E;&#x6210;&#x975E;&#x963B;&#x585E;&#x7684;&#xFF08;&#x4E0A;&#x6587;&#x4ECB;&#x7ECD;&#x8FC7;&#xFF09;&#x3002;&#x6240;&#x4EE5;&#xFF0C;&#x5982;&#x679C;&#x53D1;&#x9001;&#x4E0D;&#x4E86;&#xFF0C;&#x5E95;&#x5C42;send&#x51FD;&#x6570;&#x4F1A;&#x7ACB;&#x523B;&#x8FD4;&#x56DE;&#xFF0C;&#x5E76;&#x8FD4;&#x56DE;&#x9519;&#x8BEF;&#x7801;EINPROGRESS&#xFF08;EWOULDBLOCK&#xFF09;&#xFF0C;&#x8868;&#x660E;&#x5BF9;&#x7AEF;tcp&#x7A97;&#x53E3;&#x592A;&#x5C0F;&#xFF0C;&#x5F53;&#x524D;&#x5DF2;&#x7ECF;&#x65E0;&#x6CD5;&#x53D1;&#x51FA;&#x53BB;&#xFF1A;</p>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">bool</span> <span class="token class-name">CBaseSocket</span><span class="token operator">::</span><span class="token function">_IsBlock</span><span class="token punctuation">(</span><span class="token keyword">int</span> error_code<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">ifdef</span> <span class="token expression">_WIN32</span></span>
    <span class="token keyword">return</span> <span class="token punctuation">(</span> <span class="token punctuation">(</span>error_code <span class="token operator">==</span> WSAEINPROGRESS<span class="token punctuation">)</span> <span class="token operator">||</span> <span class="token punctuation">(</span>error_code <span class="token operator">==</span> WSAEWOULDBLOCK<span class="token punctuation">)</span> <span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">else</span></span>
    <span class="token keyword">return</span> <span class="token punctuation">(</span> <span class="token punctuation">(</span>error_code <span class="token operator">==</span> EINPROGRESS<span class="token punctuation">)</span> <span class="token operator">||</span> <span class="token punctuation">(</span>error_code <span class="token operator">==</span> EWOULDBLOCK<span class="token punctuation">)</span> <span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">endif</span></span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x8FD9;&#x4E2A;&#x65F6;&#x5019;&#xFF0C;&#x6211;&#x4EEC;&#x518D;&#x8BBE;&#x7F6E;&#x5173;&#x6CE8;&#x8BE5;socket&#x7684;&#x53EF;&#x5199;&#x4E8B;&#x4EF6;&#x3002;&#x8FD9;&#x6837;&#xFF0C;&#x4E0B;&#x6B21;&#x5BF9;&#x7AEF;tcp&#x7A97;&#x53E3;&#x5927;&#x5C0F;&#x589E;&#x5927;&#x65F6;&#xFF0C;&#x672C;&#x7AEF;&#x7684;socket&#x53EF;&#x5199;&#x65F6;&#xFF0C;&#x6211;&#x4EEC;&#x5C31;&#x80FD;&#x63A5;&#x7740;&#x53D1;&#x9001;&#x6570;&#x636E;&#x4E86;&#x3002;&#x4F1A;&#x5728;&#x670D;&#x52A1;&#x7684;&#x6D88;&#x606F;&#x6CF5;&#x4E2D;&#x68C0;&#x6D4B;&#x53EF;&#x5199;&#x4E8B;&#x4EF6;&#xFF0C;&#x63A5;&#x7740;&#x8C03;&#x7528;CBaseSocket::OnWrite()&#xFF0C; &#x8BE5;&#x51FD;&#x6570;&#x9996;&#x5148;&#x79FB;&#x9664;&#x8BE5;socket&#x7684;&#x53EF;&#x5199;&#x4E8B;&#x4EF6;&#xFF08;&#x8FD9;&#x91CC;&#x4E3A;&#x5565;&#x53EA;&#x6709;win32&#x5E73;&#x53F0;&#x548C;mac&#x673A;&#x5668;&#x79FB;&#x9664;&#x53EF;&#x5199;&#x4E8B;&#x4EF6;&#xFF0C;linux&#x5E73;&#x53F0;&#x4E0D;&#x9700;&#x8981;&#x5417;&#xFF1F;&#x4E2A;&#x4EBA;&#x89C9;&#x5F97;&#x662F;&#x7A0B;&#x5E8F;&#x4F5C;&#x8005;&#x7684;&#x758F;&#x5FFD;&#xFF09;&#x3002;</p>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">void</span> <span class="token class-name">CBaseSocket</span><span class="token operator">::</span><span class="token function">OnWrite</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">if</span> <span class="token expression"><span class="token punctuation">(</span><span class="token punctuation">(</span>defined _WIN32<span class="token punctuation">)</span> <span class="token operator">||</span> <span class="token punctuation">(</span>defined __APPLE__<span class="token punctuation">)</span><span class="token punctuation">)</span></span></span>
    <span class="token class-name">CEventDispatch</span><span class="token operator">::</span><span class="token function">Instance</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-&gt;</span><span class="token function">RemoveEvent</span><span class="token punctuation">(</span>m_socket<span class="token punctuation">,</span> SOCKET_WRITE<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">endif</span></span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span>m_state <span class="token operator">==</span> SOCKET_STATE_CONNECTING<span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
        <span class="token keyword">int</span> error <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
        socklen_t len <span class="token operator">=</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span>error<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">ifdef</span> <span class="token expression">_WIN32</span></span>

        <span class="token function">getsockopt</span><span class="token punctuation">(</span>m_socket<span class="token punctuation">,</span> SOL_SOCKET<span class="token punctuation">,</span> SO_ERROR<span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token keyword">char</span><span class="token operator">*</span><span class="token punctuation">)</span><span class="token operator">&amp;</span>error<span class="token punctuation">,</span> <span class="token operator">&amp;</span>len<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">else</span></span>
        <span class="token function">getsockopt</span><span class="token punctuation">(</span>m_socket<span class="token punctuation">,</span> SOL_SOCKET<span class="token punctuation">,</span> SO_ERROR<span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token keyword">void</span><span class="token operator">*</span><span class="token punctuation">)</span><span class="token operator">&amp;</span>error<span class="token punctuation">,</span> <span class="token operator">&amp;</span>len<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">endif</span></span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>error<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token function">m_callback</span><span class="token punctuation">(</span>m_callback_data<span class="token punctuation">,</span> NETLIB_MSG_CLOSE<span class="token punctuation">,</span> <span class="token punctuation">(</span>net_handle_t<span class="token punctuation">)</span>m_socket<span class="token punctuation">,</span> <span class="token constant">NULL</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
            m_state <span class="token operator">=</span> SOCKET_STATE_CONNECTED<span class="token punctuation">;</span>
            <span class="token function">m_callback</span><span class="token punctuation">(</span>m_callback_data<span class="token punctuation">,</span> NETLIB_MSG_CONFIRM<span class="token punctuation">,</span> <span class="token punctuation">(</span>net_handle_t<span class="token punctuation">)</span>m_socket<span class="token punctuation">,</span> <span class="token constant">NULL</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">else</span>
    <span class="token punctuation">{</span>
        <span class="token function">m_callback</span><span class="token punctuation">(</span>m_callback_data<span class="token punctuation">,</span> NETLIB_MSG_WRITE<span class="token punctuation">,</span> <span class="token punctuation">(</span>net_handle_t<span class="token punctuation">)</span>m_socket<span class="token punctuation">,</span> <span class="token constant">NULL</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x8D70;else&#x5206;&#x652F;&#xFF0C;&#x8C03;&#x7528;&#x8BBE;&#x7F6E;&#x7684;&#x56DE;&#x8C03;&#x51FD;&#x6570;imconn_callback&#xFF1A;</p>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">void</span> <span class="token function">imconn_callback</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token operator">*</span> callback_data<span class="token punctuation">,</span> <span class="token keyword">uint8_t</span> msg<span class="token punctuation">,</span> <span class="token keyword">uint32_t</span> handle<span class="token punctuation">,</span> <span class="token keyword">void</span><span class="token operator">*</span> pParam<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token function">NOTUSED_ARG</span><span class="token punctuation">(</span>handle<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">NOTUSED_ARG</span><span class="token punctuation">(</span>pParam<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>callback_data<span class="token punctuation">)</span>
        <span class="token keyword">return</span><span class="token punctuation">;</span>

    ConnMap_t<span class="token operator">*</span> conn_map <span class="token operator">=</span> <span class="token punctuation">(</span>ConnMap_t<span class="token operator">*</span><span class="token punctuation">)</span>callback_data<span class="token punctuation">;</span>
    CImConn<span class="token operator">*</span> pConn <span class="token operator">=</span> <span class="token function">FindImConn</span><span class="token punctuation">(</span>conn_map<span class="token punctuation">,</span> handle<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>pConn<span class="token punctuation">)</span>
        <span class="token keyword">return</span><span class="token punctuation">;</span>

    <span class="token comment">//log(&quot;msg=%d, handle=%d &quot;, msg, handle);</span>

    <span class="token keyword">switch</span> <span class="token punctuation">(</span>msg<span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
    <span class="token keyword">case</span> NETLIB_MSG_CONFIRM<span class="token operator">:</span>
        pConn<span class="token operator">-&gt;</span><span class="token function">OnConfirm</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">break</span><span class="token punctuation">;</span>
    <span class="token keyword">case</span> NETLIB_MSG_READ<span class="token operator">:</span>
        pConn<span class="token operator">-&gt;</span><span class="token function">OnRead</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">break</span><span class="token punctuation">;</span>
    <span class="token keyword">case</span> NETLIB_MSG_WRITE<span class="token operator">:</span>
        pConn<span class="token operator">-&gt;</span><span class="token function">OnWrite</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">break</span><span class="token punctuation">;</span>
    <span class="token keyword">case</span> NETLIB_MSG_CLOSE<span class="token operator">:</span>
        pConn<span class="token operator">-&gt;</span><span class="token function">OnClose</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">break</span><span class="token punctuation">;</span>
    <span class="token keyword">default</span><span class="token operator">:</span>
        <span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;!!!imconn_callback error msg: %d &quot;</span><span class="token punctuation">,</span> msg<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">break</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    pConn<span class="token operator">-&gt;</span><span class="token function">ReleaseRef</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x56E0;&#x4E3A;&#x8FD9;&#x6B21;&#x4F20;&#x5165;&#x7684;&#x6D88;&#x606F;&#x662F;NETLIB_MSG_WRITE&#xFF0C;&#x6240;&#x4EE5;&#x8D70;pConn-&gt;OnWrite&#x5206;&#x652F;&#xFF0C;&#x63A5;&#x7740;&#x7531;&#x4E8E;&#x591A;&#x6001;&#x8C03;&#x7528;CImConn&#x7684;&#x5B50;&#x7C7B;CProxyConn&#x7684;OnWrite()&#x51FD;&#x6570;&#xFF0C;&#x4F46;&#x7531;&#x4E8E;&#x5B50;&#x7C7B;CProxyConn&#x5E76;&#x6CA1;&#x6709;&#x6539;&#x5199;OnWrite()&#x65B9;&#x6CD5;&#xFF0C;&#x6240;&#x4EE5;&#x8C03;&#x7528;CImConn&#x7684;OnWrite():</p>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">void</span> <span class="token class-name">CImConn</span><span class="token operator">::</span><span class="token function">OnWrite</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>m_busy<span class="token punctuation">)</span>
        <span class="token keyword">return</span><span class="token punctuation">;</span>

    <span class="token keyword">while</span> <span class="token punctuation">(</span>m_out_buf<span class="token punctuation">.</span><span class="token function">GetWriteOffset</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">&gt;</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">int</span> send_size <span class="token operator">=</span> m_out_buf<span class="token punctuation">.</span><span class="token function">GetWriteOffset</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>send_size <span class="token operator">&gt;</span> NETLIB_MAX_SOCKET_BUF_SIZE<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            send_size <span class="token operator">=</span> NETLIB_MAX_SOCKET_BUF_SIZE<span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

        <span class="token keyword">int</span> ret <span class="token operator">=</span> <span class="token function">netlib_send</span><span class="token punctuation">(</span>m_handle<span class="token punctuation">,</span> m_out_buf<span class="token punctuation">.</span><span class="token function">GetBuffer</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> send_size<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>ret <span class="token operator">&lt;=</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            ret <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
            <span class="token keyword">break</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

        m_out_buf<span class="token punctuation">.</span><span class="token function">Read</span><span class="token punctuation">(</span><span class="token constant">NULL</span><span class="token punctuation">,</span> ret<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span>m_out_buf<span class="token punctuation">.</span><span class="token function">GetWriteOffset</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        m_busy <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;onWrite, remain=%d &quot;</span><span class="token punctuation">,</span> m_out_buf<span class="token punctuation">.</span><span class="token function">GetWriteOffset</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x63A5;&#x7740;&#x7EE7;&#x7EED;&#x4ECE;&#x5199;&#x7F13;&#x51B2;&#x533A;&#x53D6;&#x51FA;&#x6570;&#x636E;&#x7EE7;&#x7EED;&#x53D1;&#x9001;&#xFF0C;&#x5982;&#x679C;&#x8FD8;&#x662F;&#x53EA;&#x80FD;&#x53D1;&#x9001;&#x51FA;&#x53BB;&#xFF0C;&#x7EE7;&#x7EED;&#x76D1;&#x542C;&#x8BE5;socket&#x53EF;&#x5199;&#x4E8B;&#x4EF6;&#xFF0C;&#x6BCF;&#x6B21;&#x53D1;&#x9001;&#x51FA;&#x53BB;&#x591A;&#x5C11;&#xFF0C;&#x5C31;&#x4ECE;&#x5199;&#x7F13;&#x51B2;&#x533A;&#x4E2D;&#x79FB;&#x9664;&#x8BE5;&#x90E8;&#x5206;&#x5B57;&#x8282;&#x3002;&#x5982;&#x679C;&#x5168;&#x90E8;&#x53D1;&#x9001;&#x5B8C;&#x4E86;&#x3002;&#x5C06;&#x5FD9;&#x788C;&#x6807;&#x5FD7;m_busy&#x6E05;&#x96F6;&#xFF08;false&#xFF09;&#x3002;</p>
<p>&#x81F3;&#x6B64;&#xFF0C;&#x5E94;&#x7B54;&#x6570;&#x636E;&#x5305;&#x7684;&#x6D41;&#x7A0B;&#x4E5F;&#x4ECB;&#x7ECD;&#x5B8C;&#x4E86;&#x3002;&#x6211;&#x4EEC;&#x6765;&#x603B;&#x7ED3;&#x4E0B;&#x8BE5;&#x6D41;&#x7A0B;&#xFF1A;</p>
<pre class="language-"><code class="lang-plain">//1. &#x4E3B;&#x6D88;&#x606F;&#x6CF5;&#x68C0;&#x6D4B;&#x5230;&#x6709;&#x5176;&#x4ED6;&#x4EFB;&#x52A1;&#x9700;&#x8981;&#x505A;&#xFF0C;&#x505A;&#x4E4B;&#x3002;

//2. &#x8BE5;&#x4EFB;&#x52A1;&#x662F;&#x4ECE;&#x5168;&#x5C40;&#x7684;&#x94FE;&#x8868;&#x4E2D;&#x53D6;&#x51FA;&#x5E94;&#x7B54;&#x5305;&#x6570;&#x636E;&#xFF0C;&#x627E;&#x5230;&#x5BF9;&#x5E94;&#x7684;&#x8FDE;&#x63A5;&#x5BF9;&#x8C61;&#xFF0C;&#x7136;&#x540E;&#x5C1D;&#x8BD5;&#x76F4;&#x63A5;&#x53D1;&#x51FA;&#x53BB;&#xFF1B;

//3. &#x5982;&#x679C;&#x53D1;&#x4E0D;&#x51FA;&#xFF0C;&#x5219;&#x5C06;&#x8BE5;&#x6570;&#x636E;&#x5B58;&#x5165;&#x8BE5;&#x8FDE;&#x63A5;&#x7684;&#x53D1;&#x9001;&#x7F13;&#x51B2;&#x533A;&#xFF08;&#x5199;&#x7F13;&#x51B2;&#x533A;&#xFF09;&#xFF0C;&#x5E76;&#x76D1;&#x542C;&#x8BE5;&#x8FDE;&#x63A5;&#x7684;socket&#x53EF;&#x5199;&#x4E8B;&#x4EF6;&#x3002;

//4. &#x4E0B;&#x6B21;&#x8BE5;socket&#x89E6;&#x53D1;&#x53EF;&#x5199;&#x4E8B;&#x4EF6;&#x65F6;&#xFF0C;&#x63A5;&#x7740;&#x53D1;&#x9001;&#x8BE5;&#x8FDE;&#x63A5;&#x7684;&#x5199;&#x7F13;&#x51B2;&#x533A;&#x4E2D;&#x5269;&#x4F59;&#x7684;&#x6570;&#x636E;&#x3002;&#x5982;&#x6B64;&#x5FAA;&#x73AF;&#x76F4;&#x5230;&#x6240;&#x6709;&#x6570;&#x636E;&#x90FD;&#x53D1;&#x9001;&#x6210;&#x529F;&#x3002;

//5. &#x53D6;&#x6D88;&#x76D1;&#x542C;&#x8BE5;socket&#x53EF;&#x5199;&#x4E8B;&#x4EF6;&#xFF0C;&#x4EE5;&#x907F;&#x514D;&#x65E0;&#x6570;&#x636E;&#x7684;&#x60C5;&#x51B5;&#x4E0B;&#x89E6;&#x53D1;&#x5199;&#x4E8B;&#x4EF6;&#xFF08;&#x8BE5;&#x4E8B;&#x4EF6;&#x5927;&#x591A;&#x6570;&#x60C5;&#x51B5;&#x4E0B;&#x5F88;&#x9891;&#x7E41;&#xFF09;
</code></pre>
<p>&#x4E0A;&#x9762;&#x7684;&#x6D41;&#x7A0B;&#x4ECE;&#x7B2C;2&#x6B65;&#x5230;&#x7B2C;5&#x6B65;&#x4E5F;&#x662F;&#x4E3B;&#x6D41;&#x7F51;&#x7EDC;&#x5E93;&#x7684;&#x53D1;&#x6570;&#x636E;&#x7684;&#x903B;&#x8F91;&#x3002;&#x603B;&#x800C;&#x8A00;&#x4E4B;&#xFF0C;&#x5C31;&#x662F;&#x8BF4;&#xFF0C;&#x5148;&#x8BD5;&#x7740;&#x53D1;&#x9001;&#x6570;&#x636E;&#xFF0C;&#x5982;&#x679C;&#x53D1;&#x4E0D;&#x51FA;&#x53BB;&#xFF0C;&#x5B58;&#x8D77;&#x6765;&#xFF0C;&#x76D1;&#x542C;&#x53EF;&#x5199;&#x4E8B;&#x4EF6;&#xFF0C;&#x4E0B;&#x6B21;&#x89E6;&#x53D1;&#x53EF;&#x5199;&#x4E8B;&#x4EF6;&#x540E;&#x63A5;&#x7740;&#x53D1;&#x3002;&#x4E00;&#x76F4;&#x5230;&#x6570;&#x636E;&#x5168;&#x90E8;&#x53D1;&#x51FA;&#x53BB;&#x540E;&#xFF0C;&#x79FB;&#x9664;&#x76D1;&#x542C;&#x53EF;&#x5199;&#x4E8B;&#x4EF6;&#x3002;&#x901A;&#x5E38;&#x53EA;&#x8981;&#x53EF;&#x5199;&#x4E8B;&#x4EF6;&#x662F;&#x4E0D;&#x65AD;&#x4F1A;&#x89E6;&#x53D1;&#x7684;&#xFF0C;&#x6240;&#x4EE5;&#x9ED8;&#x8BA4;&#x4E0D;&#x76D1;&#x542C;&#x53EF;&#x5199;&#x4E8B;&#x4EF6;&#xFF0C;&#x53EA;&#x6709;&#x6570;&#x636E;&#x53D1;&#x4E0D;&#x51FA;&#x7684;&#x65F6;&#x5019;&#x624D;&#x4F1A;&#x76D1;&#x542C;&#x53EF;&#x5199;&#x4E8B;&#x4EF6;&#x3002;&#x8FD9;&#x4E2A;&#x539F;&#x5219;&#xFF0C;&#x5343;&#x4E07;&#x8981;&#x8BB0;&#x4F4F;&#x3002;</p>
<p>&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x95EE;&#x9898;&#xFF0C;&#x662F;&#x5173;&#x4E8E;&#x5FC3;&#x8DF3;&#x5305;&#x7684;&#xFF0C;&#x5373;db_proxy_server&#x662F;&#x5982;&#x4F55;&#x53D1;&#x9001;&#x5FC3;&#x8DF3;&#x5305;&#x7684;&#xFF1A;</p>
<p>&#x7A0B;&#x5E8F;&#x521D;&#x59CB;&#x5316;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x6CE8;&#x518C;&#x4E00;&#x4E2A;&#x5B9A;&#x65F6;&#x5668;&#x51FD;&#x6570;&#xFF1A;</p>
<pre class="language-"><code class="lang-cpp"><span class="token function">init_proxy_conn</span><span class="token punctuation">(</span>thread_num<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">int</span> <span class="token function">init_proxy_conn</span><span class="token punctuation">(</span><span class="token keyword">uint32_t</span> thread_num<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    s_handler_map <span class="token operator">=</span> <span class="token class-name">CHandlerMap</span><span class="token operator">::</span><span class="token function">getInstance</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    g_thread_pool<span class="token punctuation">.</span><span class="token function">Init</span><span class="token punctuation">(</span>thread_num<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token function">netlib_add_loop</span><span class="token punctuation">(</span>proxy_loop_callback<span class="token punctuation">,</span> <span class="token constant">NULL</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token function">signal</span><span class="token punctuation">(</span>SIGTERM<span class="token punctuation">,</span> sig_handler<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">return</span> <span class="token function">netlib_register_timer</span><span class="token punctuation">(</span>proxy_timer_callback<span class="token punctuation">,</span> <span class="token constant">NULL</span><span class="token punctuation">,</span> <span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x6700;&#x540E;&#x4E00;&#x884C;&#xFF1A;return netlib_register_timer(proxy_timer_callback, NULL, 1000);</p>
<p>&#x7136;&#x540E;&#x5728;&#x6D88;&#x606F;&#x6CF5;&#x91CC;&#x9762;&#x68C0;&#x6D4B;&#x5B9A;&#x65F6;&#x5668;&#xFF1A;</p>
<pre class="language-"><code class="lang-cpp"><span class="token function">_CheckTimer</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">void</span> <span class="token class-name">CEventDispatch</span><span class="token operator">::</span><span class="token function">_CheckTimer</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token keyword">uint64_t</span> curr_tick <span class="token operator">=</span> <span class="token function">get_tick_count</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    list<span class="token operator">&lt;</span>TimerItem<span class="token operator">*</span><span class="token operator">&gt;</span><span class="token operator">::</span>iterator it<span class="token punctuation">;</span>

    <span class="token keyword">for</span> <span class="token punctuation">(</span>it <span class="token operator">=</span> m_timer_list<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> it <span class="token operator">!=</span> m_timer_list<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
        TimerItem<span class="token operator">*</span> pItem <span class="token operator">=</span> <span class="token operator">*</span>it<span class="token punctuation">;</span>
        it<span class="token operator">++</span><span class="token punctuation">;</span>        <span class="token comment">// iterator maybe deleted in the callback, so we should increment it before callback</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>curr_tick <span class="token operator">&gt;=</span> pItem<span class="token operator">-&gt;</span>next_tick<span class="token punctuation">)</span>
        <span class="token punctuation">{</span>
            pItem<span class="token operator">-&gt;</span>next_tick <span class="token operator">+=</span> pItem<span class="token operator">-&gt;</span>interval<span class="token punctuation">;</span>
            pItem<span class="token operator">-&gt;</span><span class="token function">callback</span><span class="token punctuation">(</span>pItem<span class="token operator">-&gt;</span>user_data<span class="token punctuation">,</span> NETLIB_MSG_TIMER<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token constant">NULL</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">uint64_t</span> <span class="token function">get_tick_count</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">ifdef</span> <span class="token expression">_WIN32</span></span>
    LARGE_INTEGER liCounter<span class="token punctuation">;</span> 
    LARGE_INTEGER liCurrent<span class="token punctuation">;</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span><span class="token function">QueryPerformanceFrequency</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>liCounter<span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token keyword">return</span> <span class="token function">GetTickCount</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token function">QueryPerformanceCounter</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>liCurrent<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">uint64_t</span><span class="token punctuation">)</span><span class="token punctuation">(</span>liCurrent<span class="token punctuation">.</span>QuadPart <span class="token operator">*</span> <span class="token number">1000</span> <span class="token operator">/</span> liCounter<span class="token punctuation">.</span>QuadPart<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">else</span></span>
    <span class="token keyword">struct</span> <span class="token class-name">timeval</span> tval<span class="token punctuation">;</span>
    <span class="token keyword">uint64_t</span> ret_tick<span class="token punctuation">;</span>

    <span class="token function">gettimeofday</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>tval<span class="token punctuation">,</span> <span class="token constant">NULL</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    ret_tick <span class="token operator">=</span> tval<span class="token punctuation">.</span>tv_sec <span class="token operator">*</span> <span class="token number">1000L</span> <span class="token operator">+</span> tval<span class="token punctuation">.</span>tv_usec <span class="token operator">/</span> <span class="token number">1000L</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> ret_tick<span class="token punctuation">;</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">endif</span></span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x7531;&#x4E0A;&#x9762;&#x7684;&#x51FD;&#x6570;&#x53EF;&#x4EE5;&#x770B;&#x51FA;&#x6765;&#x5B9A;&#x65F6;&#x5668;&#x7684;&#x5355;&#x4F4D;&#x662F;&#x6BEB;&#x79D2;&#xFF0C;&#x5F53;&#x5B9A;&#x65F6;&#x5668;&#x65F6;&#x95F4;&#x5230;&#x4E86;&#x540E;&#xFF0C;&#x8C03;&#x7528;&#x56DE;&#x8C03;&#x51FD;&#x6570;proxy_timer_callback&#xFF1A;</p>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">void</span> <span class="token function">proxy_timer_callback</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token operator">*</span> callback_data<span class="token punctuation">,</span> <span class="token keyword">uint8_t</span> msg<span class="token punctuation">,</span> <span class="token keyword">uint32_t</span> handle<span class="token punctuation">,</span> <span class="token keyword">void</span><span class="token operator">*</span> pParam<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token keyword">uint64_t</span> cur_time <span class="token operator">=</span> <span class="token function">get_tick_count</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span>ConnMap_t<span class="token operator">::</span>iterator it <span class="token operator">=</span> g_proxy_conn_map<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> it <span class="token operator">!=</span> g_proxy_conn_map<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span>
        ConnMap_t<span class="token operator">::</span>iterator it_old <span class="token operator">=</span> it<span class="token punctuation">;</span>
        it<span class="token operator">++</span><span class="token punctuation">;</span>

        CProxyConn<span class="token operator">*</span> pConn <span class="token operator">=</span> <span class="token punctuation">(</span>CProxyConn<span class="token operator">*</span><span class="token punctuation">)</span>it_old<span class="token operator">-&gt;</span>second<span class="token punctuation">;</span>
        pConn<span class="token operator">-&gt;</span><span class="token function">OnTimer</span><span class="token punctuation">(</span>cur_time<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">void</span> <span class="token class-name">CProxyConn</span><span class="token operator">::</span><span class="token function">OnTimer</span><span class="token punctuation">(</span><span class="token keyword">uint64_t</span> curr_tick<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>curr_tick <span class="token operator">&gt;</span> m_last_send_tick <span class="token operator">+</span> SERVER_HEARTBEAT_INTERVAL<span class="token punctuation">)</span> <span class="token punctuation">{</span>

        CImPdu cPdu<span class="token punctuation">;</span>
        IM<span class="token operator">::</span>Other<span class="token operator">::</span>IMHeartBeat msg<span class="token punctuation">;</span>
        cPdu<span class="token punctuation">.</span><span class="token function">SetPBMsg</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>msg<span class="token punctuation">)</span><span class="token punctuation">;</span>
        cPdu<span class="token punctuation">.</span><span class="token function">SetServiceId</span><span class="token punctuation">(</span>IM<span class="token operator">::</span>BaseDefine<span class="token operator">::</span>SID_OTHER<span class="token punctuation">)</span><span class="token punctuation">;</span>
        cPdu<span class="token punctuation">.</span><span class="token function">SetCommandId</span><span class="token punctuation">(</span>IM<span class="token operator">::</span>BaseDefine<span class="token operator">::</span>CID_OTHER_HEARTBEAT<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">SendPdu</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>cPdu<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span>curr_tick <span class="token operator">&gt;</span> m_last_recv_tick <span class="token operator">+</span> SERVER_TIMEOUT<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;proxy connection timeout %s:%d&quot;</span><span class="token punctuation">,</span> m_peer_ip<span class="token punctuation">.</span><span class="token function">c_str</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> m_peer_port<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">Close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>m_last_send_tick&#x662F;&#x4E0A;&#x4E00;&#x6B21;&#x53D1;&#x9001;&#x6570;&#x636E;&#x7684;&#x65F6;&#x95F4;&#xFF0C;&#x6211;&#x4EEC;&#x4E0A;&#x6587;&#x4E2D;&#x4ECB;&#x7ECD;&#x8FC7;&#xFF0C;&#x5982;&#x679C;&#x5F53;&#x524D;&#x65F6;&#x95F4;&#x8DDD;&#x4E0A;&#x4E00;&#x6B21;&#x53D1;&#x9001;&#x6570;&#x636E;&#x7684;&#x65F6;&#x95F4;&#x5DF2;&#x7ECF;&#x8D85;&#x8FC7;&#x4E86;&#x6307;&#x5B9A;&#x7684;&#x65F6;&#x95F4;&#x95F4;&#x9694;&#xFF0C;&#x5219;&#x53D1;&#x9001;&#x4E00;&#x4E2A;&#x5FC3;&#x8DF3;&#x5305;&#xFF08;&#x8FD9;&#x91CC;&#x7684;&#x65F6;&#x95F4;&#x95F4;&#x9694;&#x662F;5000&#x6BEB;&#x79D2;&#xFF09;&#x3002;</p>
<p>m_last_recv_tick&#x662F;&#x4E0A;&#x4E00;&#x6B21;&#x6536;&#x53D6;&#x6570;&#x636E;&#x7684;&#x65F6;&#x95F4;&#xFF0C;&#x6211;&#x4EEC;&#x4E0A;&#x6587;&#x4E5F;&#x4ECB;&#x7ECD;&#x8FC7;&#xFF0C;&#x5982;&#x679C;&#x5F53;&#x524D;&#x65F6;&#x95F4;&#x4E3E;&#x4F8B;&#x4E0A;&#x4E00;&#x6B21;&#x63A5;&#x6536;&#x65F6;&#x95F4;&#x5DF2;&#x7ECF;&#x8D85;&#x8FC7;&#x4E86;&#x6307;&#x5B9A;&#x7684;&#x65F6;&#x95F4;&#x95F4;&#x9694;&#xFF08;&#x76F8;&#x5F53;&#x4E8E;&#x4E00;&#x6BB5;&#x65F6;&#x95F4;&#x5185;&#xFF0C;&#x5BF9;&#x7AEF;&#x6CA1;&#x6709;&#x7ED9;&#x5F53;&#x524D;&#x670D;&#x52A1;&#x53D1;&#x9001;&#x4EFB;&#x4F55;&#x6570;&#x636E;&#xFF09;&#xFF0C;&#x8FD9;&#x4E2A;&#x65F6;&#x5019;&#x5C31;&#x5173;&#x95ED;&#x8BE5;&#x8FDE;&#x63A5;&#xFF08;&#x8FD9;&#x91CC;&#x8BBE;&#x7F6E;&#x7684;&#x65F6;&#x95F4;&#x95F4;&#x9694;&#x662F;30000&#x6BEB;&#x79D2;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;30&#x79D2;&#xFF09;&#x3002;</p>
<p>&#x8FD9;&#x79CD;&#x5FC3;&#x8DF3;&#x5305;&#x673A;&#x5236;&#x7279;&#x522B;&#x503C;&#x5F97;&#x63A8;&#x5D07;&#xFF0C;&#x4E5F;&#x662F;&#x5E38;&#x89C1;&#x7684;&#x5FC3;&#x8DF3;&#x5305;&#x7B56;&#x7565;&#x3002;</p>
<p>&#x81F3;&#x6B64;&#xFF0C;db_proxy_server&#x7684;&#x6846;&#x67B6;&#x548C;&#x539F;&#x7406;&#x4E5F;&#x5C31;&#x4ECB;&#x7ECD;&#x5B8C;&#x4E86;&#x3002;&#x5269;&#x4E0B;&#x7684;&#x5C31;&#x662F;&#x4E00;&#x4E9B;&#x4E1A;&#x52A1;&#x903B;&#x8F91;&#x4E86;&#x3002;&#x5982;&#x679C;&#x4F60;&#x611F;&#x5174;&#x8DA3;&#xFF0C;&#x53EF;&#x4EE5;&#x81EA;&#x5DF1;&#x67E5;&#x770B;&#x5BF9;&#x5E94;&#x7684;&#x547D;&#x4EE4;&#x53F7;&#x7ED1;&#x5B9A;&#x7684;&#x5904;&#x7406;&#x51FD;&#x6570;&#x7684;&#x5904;&#x7406;&#x6D41;&#x7A0B;&#x3002;</p>
<p>&#x6587;&#x4E2D;&#x5982;&#x679C;&#x6709;&#x8BF4;&#x9519;&#x7684;&#x5730;&#x65B9;&#xFF0C;&#x6B22;&#x8FCE;&#x63D0;&#x51FA;&#x7559;&#x8A00;&#x6307;&#x6B63;&#x3002; </p>
<footer class="page-footer"><span class="copyright">&#x5982;&#x9700;&#x4E0B;&#x8F7D;&#x672C;&#x7AD9;&#x5168;&#x90E8;&#x6280;&#x672F;&#x6587;&#x7AE0;&#xFF0C;&#x53EF;&#x4EE5;&#x5728;&#x3010;&#x9AD8;&#x6027;&#x80FD;&#x670D;&#x52A1;&#x5668;&#x5F00;&#x53D1;&#x3011;&#x516C;&#x4F17;&#x53F7;&#x56DE;&#x590D;&#x5173;&#x952E;&#x5B57;&#x201C;&#x6587;&#x7AE0;&#x4E0B;&#x8F7D;&#x201D;&#x5373;&#x53EF;&#x3002;</span><span class="footer-modification">&#x6700;&#x8FD1;&#x66F4;&#x65B0;&#x65F6;&#x95F4;&#xFF1A;
2020-10-04 14:34:33
</span></footer>
<script>console.log("plugin-popup....");document.onclick = function(e){ e.target.tagName === "IMG" && window.open(e.target.src,e.target.src)}</script><style>img{cursor:pointer}</style>
						
					</section>
					
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

				</div>
			</div>
			
		</div>

		
		
		<a href="03服务器端的程序架构介绍.html" class="navigation navigation-prev " aria-label="Previous page: 03 服务器端的程序架构介绍">
			<i class="fa fa-angle-left"></i>
		</a>
		
		
		<a href="05服务器端msg_server源码分析.html" class="navigation navigation-next " aria-label="Next page: 05 服务器端msg_server源码分析">
			<i class="fa fa-angle-right"></i>
		</a>
		
		
		
	</div>
	<script>
		var gitbook = gitbook || [];
		gitbook.push(function() {
			gitbook.page.hasChanged({"page":{"title":"04 服务器端db_proxy_server源码分析","level":"6.1.4","depth":2,"next":{"title":"05 服务器端msg_server源码分析","level":"6.1.5","depth":2,"path":"articles/TeamTalk源码解析/05服务器端msg_server源码分析.md","ref":"articles/TeamTalk源码解析/05服务器端msg_server源码分析.md","articles":[]},"previous":{"title":"03 服务器端的程序架构介绍","level":"6.1.3","depth":2,"path":"articles/TeamTalk源码解析/03服务器端的程序架构介绍.md","ref":"articles/TeamTalk源码解析/03服务器端的程序架构介绍.md","articles":[]},"dir":"ltr"},"config":{"plugins":["chapter-fold","-lunr","-search","search-pro","splitter","popup","back-to-top-button","hide-element","prism","-highlight","tbfed-pagefooter","code","anchor-navigation-ex","theme-fexa","livereload"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"如需下载本站全部技术文章，可以在【高性能服务器开发】公众号回复关键字“文章下载”即可。","modify_label":"最近更新时间：","modify_format":"YYYY-MM-DD HH:mm:ss"},"chapter-fold":{},"prism":{},"livereload":{},"splitter":{},"search-pro":{},"popup":{},"theme-fexa":{"search-placeholder":"输入关键字搜索","logo":"./logo.png","favicon":"./favicon.ico"},"code":{"copyButtons":true},"hide-element":{"elements":[".gitbook-link"]},"fontsettings":{"theme":"white","family":"sans","size":2},"anchor-navigation-ex":{"showLevel":false,"associatedWithSummary":true,"mode":"float","showGoTop":false,"printLog":false,"multipleH1":true,"float":{"floatIcon":"fa fa-navicon","showLevelIcon":false,"level1Icon":"","level2Icon":"","level3Icon":""},"pageTop":{"showLevelIcon":false,"level1Icon":"","level2Icon":"","level3Icon":""}},"back-to-top-button":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":"easy_coder","pdf":{"pageBreaksBefore":"/","headerTemplate":null,"paperSize":"a4","margin":{"right":62,"left":62,"top":36,"bottom":36},"fontSize":12,"fontFamily":"Arial","footerTemplate":null,"chapterMark":"pagebreak","pageNumbers":false},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"isbn":"","variables":{},"title":"高性能服务器开发 技术专栏","links":{},"gitbook":"*","description":"","theme-default":{"showLevel":true},"extension":null},"file":{"path":"articles/TeamTalk源码解析/04服务器端db_proxy_server源码分析.md","mtime":"2020-10-04T06:34:33.386Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-10-09T15:31:57.905Z"},"basePath":"../..","book":{"language":""}});
		});
	</script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-chapter-fold/chapter-fold.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search-pro/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-back-to-top-button/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-hide-element/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-code/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-theme-fexa/fexa.js"></script>
        
    

    </body>
</html>

